diff --git a/apps/users/src/infrastructure/controller/user.controller.ts b/apps/users/src/infrastructure/controller/user.controller.ts index ee5fdb3..ecf0e45 100644 --- a/apps/users/src/infrastructure/controller/user.controller.ts +++ b/apps/users/src/infrastructure/controller/user.controller.ts @@ -40,14 +40,14 @@ export class UserController { return user } - @Get('user/:userId/avatar') - async getAvatarByUserId(@Param('userId', ParseIntPipe) userId: number) { + @MessagePattern(RMQ_CMD.GET_AVATAR_BY_ID) + async getAvatarByUserId(@Payload() userId: number) { const user = await this.getAvatarUsecaseProxy.getInstance().getAvatar(userId) return user } - @Delete('user/:userId/avatar') - async deleteAvatar(@Param('userId', ParseIntPipe) userId: number) { + @MessagePattern(RMQ_CMD.DELETE_AVATAR_BY_ID) + async deleteAvatar(@Payload() userId: number) { return this.deleteAvatarUsecase.getInstance().deleteAvatar(userId) } diff --git a/apps/users/src/infrastructure/exceptions/exceptions.service.ts b/apps/users/src/infrastructure/exceptions/exceptions.service.ts index 496882b..6b57e9a 100644 --- a/apps/users/src/infrastructure/exceptions/exceptions.service.ts +++ b/apps/users/src/infrastructure/exceptions/exceptions.service.ts @@ -1,22 +1,29 @@ -import { Injectable } from '@nestjs/common'; +import { + BadRequestException, + ForbiddenException, + HttpException, + Injectable, + InternalServerErrorException, + UnauthorizedException, + ConflictException +} from '@nestjs/common'; import { IExceptionService, IFormatExceptionMessage, IFormatCommonExceptionMessage } from '../../domain/exceptions/exception-service.interface'; -import { RpcException } from '@nestjs/microservices'; @Injectable() export class ExceptionsService implements IExceptionService { requestException(data: IFormatCommonExceptionMessage): void { - throw new RpcException({ message: data.message, status: data.code_error }) + throw new HttpException(data.message, data.code_error); } badRequestException({ message }: IFormatExceptionMessage): void { - throw new RpcException({ message, status: 400 }) + throw new BadRequestException(message); } internalServerErrorException({ message }: IFormatExceptionMessage): void { - throw new RpcException({ message, status: 500 }) + throw new InternalServerErrorException(message); } forbiddenException({ message }: IFormatExceptionMessage): void { - throw new RpcException({ message, status: 403 }) + throw new ForbiddenException(message); } conflictException({ message }: IFormatExceptionMessage): void { - throw new RpcException({ message, status: 409 }) + throw new ConflictException(message); } -} +} \ No newline at end of file diff --git a/apps/users/src/usecase/create-user.usecase.ts b/apps/users/src/usecase/create-user.usecase.ts index ae9d93f..35e0afd 100644 --- a/apps/users/src/usecase/create-user.usecase.ts +++ b/apps/users/src/usecase/create-user.usecase.ts @@ -1,4 +1,3 @@ -import { IAppMailer } from "../domain/app-mailer/app-mailer.abstract"; import { IExceptionService } from "../domain/exceptions/exception-service.interface"; import { IMessageBrokerService } from "../domain/message-broker/message-broker.interface"; import { UserM } from "../domain/model/user"; @@ -12,10 +11,9 @@ export class CreateUserUsecase { ) { } // it would be better to use transactions here . - // unfortunately TYPEORM/MongoDB doesn't support it yet. async createUser(newUser: UserM) { const exist = await this.userDataSource.findByEmail(newUser.email) - if (exist) throw this.exceptionService.conflictException({ message: 'Email Already Token' }) + if (exist) throw this.exceptionService.badRequestException({ message: 'Email Already Token' }) const createdUser = await this.userDataSource.insert(newUser) this.messageBroker.emitUserCreatedEvent(createdUser) return createdUser diff --git a/libs/common/src/filter/exception.filter.ts b/libs/common/src/filter/exception.filter.ts index fa4b023..0bca4c8 100644 --- a/libs/common/src/filter/exception.filter.ts +++ b/libs/common/src/filter/exception.filter.ts @@ -1,8 +1,8 @@ -import { ArgumentsHost, Catch, ExceptionFilter, HttpException, BadRequestException, HttpStatus, Logger } from '@nestjs/common'; +import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus } from '@nestjs/common'; import { Request, Response } from 'express'; -import { QueryFailedError, EntityNotFoundError, CannotCreateEntityIdMapError, TypeORMError } from 'typeorm'; -import { MongoBulkWriteError, MongoError } from 'mongodb' +import { TypeORMError } from 'typeorm'; +import { MongoError } from 'mongodb' import { AxiosError } from 'axios' import { IResponseError } from '../interface/error-response.interface'; @@ -14,11 +14,10 @@ export class GlobalExceptionFilter implements ExceptionFilter { const request = ctx.getRequest(); let message = (exception as any)?.message; - let code: string = 'Exception'; - let status: number = HttpStatus.INTERNAL_SERVER_ERROR; + let code: string = (exception as any)?.response?.error || 'Exception'; + let status: number = (exception as any)?.response?.statusCode || HttpStatus.INTERNAL_SERVER_ERROR; const ec = exception - if (ec instanceof HttpException) { status = (exception as HttpException).getStatus(); message = (exception as any).getResponse()?.message ? (exception as any)?.getResponse().message : (exception as any).getResponse() @@ -39,15 +38,6 @@ export class GlobalExceptionFilter implements ExceptionFilter { message = (exception as AxiosError)?.message; code = (exception as AxiosError)?.code; } - else if ((ec as any).message && (ec as any).status) { - status = (exception as any)?.status - message = (exception as any)?.message; - code = (exception as any)?.status; - } - else { - status = HttpStatus.INTERNAL_SERVER_ERROR - } - response.status(status).json(globalResponseError(status, message, code, request)); } }