diff --git a/src/dto/general-error-response.dto.ts b/src/dto/general-error-response.dto.ts new file mode 100644 index 0000000..da46c35 --- /dev/null +++ b/src/dto/general-error-response.dto.ts @@ -0,0 +1,9 @@ +export class GeneralErrorResponse { + constructor(_error_code: number = 0, _detail: any = null) { + this.error_code = _error_code; + this.detail = _detail; + return this; + } + error_code: number; + detail: any; +} diff --git a/src/dto/general-service-response.dto.ts b/src/dto/general-service-response.dto.ts new file mode 100644 index 0000000..777ae89 --- /dev/null +++ b/src/dto/general-service-response.dto.ts @@ -0,0 +1,8 @@ +export class GeneralServiceResponse { + constructor() { + this.statusCode = null; + this.data = null; + } + statusCode: number; + data: any; +} diff --git a/src/exceptions/custom-bad-request.exception.ts b/src/exceptions/custom-bad-request.exception.ts new file mode 100644 index 0000000..3b9b689 --- /dev/null +++ b/src/exceptions/custom-bad-request.exception.ts @@ -0,0 +1,8 @@ +import { BadRequestException } from '@nestjs/common'; +import { GeneralErrorResponse } from 'src/dto/general-error-response.dto'; + +export class CustomBadRequestException extends BadRequestException { + constructor(public readonly error_response: GeneralErrorResponse) { + super(error_response); + } +} diff --git a/src/exceptions/custom-not-found.exception.ts b/src/exceptions/custom-not-found.exception.ts new file mode 100644 index 0000000..8356509 --- /dev/null +++ b/src/exceptions/custom-not-found.exception.ts @@ -0,0 +1,8 @@ +import { NotFoundException } from '@nestjs/common'; +import { GeneralErrorResponse } from 'src/dto/general-error-response.dto'; + +export class CustomNotFoundException extends NotFoundException { + constructor(public readonly error_response: GeneralErrorResponse) { + super(error_response); + } +} diff --git a/src/feature/restaurant/dto/send-contact-form-response.dto.ts b/src/feature/restaurant/dto/send-contact-form-response.dto.ts index 7169877..2afa748 100644 --- a/src/feature/restaurant/dto/send-contact-form-response.dto.ts +++ b/src/feature/restaurant/dto/send-contact-form-response.dto.ts @@ -1,3 +1,6 @@ -import { GeneralResponse } from 'src/dto/general-response.dto'; - -export class SendContactFormResponse extends GeneralResponse {} +export class SendContactFormResponse { + constructor(_message: string = '') { + this.message = _message; + } + message: string; +} diff --git a/src/feature/restaurant/restaurant.controller.ts b/src/feature/restaurant/restaurant.controller.ts index c18e058..0740f68 100644 --- a/src/feature/restaurant/restaurant.controller.ts +++ b/src/feature/restaurant/restaurant.controller.ts @@ -7,6 +7,9 @@ import { GetRestaurantDetailResponse } from './dto/get-restaurant-detail-respons import { SendContactFormRequest } from './dto/send-contact-form-request.dto'; import { SendContactFormResponse } from './dto/send-contact-form-response.dto'; import { CommonService } from '../common/common.service'; +import { GeneralServiceResponse } from 'src/dto/general-service-response.dto'; +import { GeneralErrorResponse } from 'src/dto/general-error-response.dto'; +import { CustomBadRequestException } from 'src/exceptions/custom-bad-request.exception'; @Controller() export class RestaurantController { @@ -50,40 +53,45 @@ export class RestaurantController { @MessagePattern({ cmd: 'send_contact_form' }) async sendContactForm( data: SendContactFormRequest, - ): Promise { - const res = new SendContactFormResponse(200, ''); + ): Promise { + const res = new GeneralServiceResponse(); const { email, message } = data; try { if (!email) { - throw new HttpException('Email is required', 400); + // throw new HttpException('Email is required', 400); + throw new CustomBadRequestException( + new GeneralErrorResponse(1, 'Email is required'), + ); } if (!message) { - throw new HttpException('Message is required', 400); + // throw new HttpException('Message is required', 400); + throw new CustomBadRequestException( + new GeneralErrorResponse(1, 'Message is required'), + ); } //Validate Email Format if (!this.commonService.validateEmail(email)) { - throw new HttpException('Invalid Email Format', 400); + // throw new HttpException('Invalid Email Format', 400); + throw new CustomBadRequestException( + new GeneralErrorResponse(1, 'Invalid Email Format'), + ); } //Process data - const conact_id = - await this.restaurantService.sendContactFormFromEndPoint( - email, - message, - ); + await this.restaurantService.sendContactFormFromEndPoint(email, message); + res.statusCode = 200; - res.message = 'Sending Contact Form Successfully'; - res.data = conact_id; + res.data = new SendContactFormResponse( + 'Sending Contact Form Successfully', + ); } catch (error) { if (error instanceof HttpException) { res.statusCode = error.getStatus(); - res.message = error.getResponse(); - res.data = null; + res.data = error.getResponse(); } else { res.statusCode = 500; - res.message = error.toString(); - res.data = null; + res.data = new GeneralErrorResponse(9, error.toString()); } }