Skip to content

Commit

Permalink
rollback exception filter
Browse files Browse the repository at this point in the history
  • Loading branch information
morteza-mortezai committed Jun 5, 2023
1 parent bccbf85 commit 8e7516e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
8 changes: 4 additions & 4 deletions apps/users/src/infrastructure/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
23 changes: 15 additions & 8 deletions apps/users/src/infrastructure/exceptions/exceptions.service.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
4 changes: 1 addition & 3 deletions apps/users/src/usecase/create-user.usecase.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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
Expand Down
20 changes: 5 additions & 15 deletions libs/common/src/filter/exception.filter.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -14,11 +14,10 @@ export class GlobalExceptionFilter implements ExceptionFilter {
const request = ctx.getRequest<Request>();

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()
Expand All @@ -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));
}
}
Expand Down

0 comments on commit 8e7516e

Please sign in to comment.