import { ArgumentsHost, BadGatewayException, BadRequestException, ConflictException, ExceptionFilter, ForbiddenException, GatewayTimeoutException, GoneException, HttpStatus, HttpVersionNotSupportedException, ImATeapotException, InternalServerErrorException, MethodNotAllowedException, NotAcceptableException, NotFoundException, NotImplementedException, PayloadTooLargeException, PreconditionFailedException, RequestTimeoutException, ServiceUnavailableException, UnauthorizedException, UnprocessableEntityException, UnsupportedMediaTypeException, } from '@nestjs/common'; import { HandledException } from '../exceptions/custom.exception'; import { RESTError } from '../models/wrappers/rest-wrapper.class'; import { mongo } from 'mongoose'; import { ValidationError } from 'class-validator'; import { AppLogger } from '../modules/core/services/app-logger.service'; export class BaseExceptionsFilter implements ExceptionFilter { constructor(private readonly _logger: AppLogger) { this._logger.log('### BaseExceptionsFilter - Initialized ###'); } catch(exception: any, host: ArgumentsHost) { const context = host.switchToHttp(); const response = context.getResponse(); this._logger.log('### BaseExceptionsFilter - catch ###'); if (exception && exception.constructor && exception.constructor.name) { this._logger.error(`BaseExceptionsFilter - catch`, exception.constructor.name); } try { if (typeof exception !== 'string') { this._logger.error(`trace`, JSON.stringify(exception)); } else { this._logger.error(`trace`, exception); } } catch (e) { this._logger.debug('!!!! BaseExceptionsFilter Logger Error !!!!'); } let handledException: HandledException | any = null; const { restErrors, statusCode } = this.filterException(exception); handledException = new HandledException(restErrors, statusCode); handledException.send(response); } private filterException(exception: any) { const restErrors: RESTError[] = []; let statusCode: HttpStatus = null; try { if (exception instanceof HandledException) { //todo // return exception; restErrors.push(...exception.getErrors()); statusCode = exception.getStatus() || HttpStatus.BAD_REQUEST; } else if (exception instanceof RESTError) { // handledException = new HandledException( // [exception], // exception.statusCode || HttpStatus.BAD_REQUEST, // ); restErrors.push(exception); statusCode = exception.statusCode || HttpStatus.BAD_REQUEST; } else if (exception instanceof Array) { exception.forEach((item) => { restErrors.push(...this.filterException(item).restErrors); }); statusCode = HttpStatus.BAD_REQUEST; } else if (exception instanceof BadRequestException) { restErrors.push(new RESTError('BadRequestException')); statusCode = HttpStatus.BAD_REQUEST; } else if (exception instanceof UnauthorizedException) { restErrors.push(new RESTError('UnauthorizedException')); statusCode = HttpStatus.UNAUTHORIZED; } else if (exception instanceof NotFoundException) { restErrors.push(new RESTError('NOT_FOUND', exception.message)); statusCode = HttpStatus.NOT_FOUND; } else if (exception instanceof ForbiddenException) { restErrors.push( new RESTError( 'INVALID_API_TOKEN', 'invalid X-API-TOKEN received in headers', ), ); statusCode = HttpStatus.FORBIDDEN; } else if (exception instanceof NotAcceptableException) { restErrors.push(new RESTError('NotAcceptableException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof RequestTimeoutException) { restErrors.push(new RESTError('RequestTimeoutException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof ConflictException) { restErrors.push(new RESTError('ConflictException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof GoneException) { restErrors.push(new RESTError('GoneException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof HttpVersionNotSupportedException) { restErrors.push(new RESTError('HttpVersionNotSupportedException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof PayloadTooLargeException) { restErrors.push(new RESTError('PayloadTooLargeException')); statusCode = HttpStatus.PAYLOAD_TOO_LARGE; } else if (exception instanceof UnsupportedMediaTypeException) { restErrors.push(new RESTError('UnsupportedMediaTypeException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof UnprocessableEntityException) { restErrors.push(new RESTError('UnprocessableEntityException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof InternalServerErrorException) { restErrors.push(new RESTError('InternalServerErrorException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof NotImplementedException) { restErrors.push(new RESTError('NotImplementedException')); statusCode = HttpStatus.NOT_IMPLEMENTED; } else if (exception instanceof ImATeapotException) { restErrors.push(new RESTError('ImATeapotException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof MethodNotAllowedException) { restErrors.push(new RESTError('MethodNotAllowedException')); statusCode = HttpStatus.METHOD_NOT_ALLOWED; } else if (exception instanceof BadGatewayException) { restErrors.push(new RESTError('BadGatewayException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof ServiceUnavailableException) { restErrors.push(new RESTError('ServiceUnavailableException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof GatewayTimeoutException) { restErrors.push(new RESTError('GatewayTimeoutException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof PreconditionFailedException) { restErrors.push(new RESTError('PreconditionFailedException')); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof TypeError) { restErrors.push(new RESTError('TYPE_ERROR', exception.message)); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof mongo.MongoError) { restErrors.push(new RESTError('MONGO_ERROR', exception.message)); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; // } else if (exception instanceof CastError) { // handledException = new HandledException( // [new RESTError('CAST_ERROR', exception.message)], // HttpStatus.INTERNAL_SERVER_ERROR, // ); } else if (exception instanceof ValidationError) { restErrors.push(new RESTError('VALIDATION_ERROR', exception)); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else if (exception instanceof mongo.MongoServerError) { restErrors.push(new RESTError('MONGO_SERVER_ERROR', exception.message)); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } else { restErrors.push(new RESTError('UNKNOWN_EXCEPTION', exception.message)); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } } catch (ex) { restErrors.push(new RESTError('FILTER_EXCEPTION', ex)); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } return { restErrors, statusCode }; } }