add app filter

This commit is contained in:
2025-05-23 15:57:05 +08:00
parent 91dcac731d
commit 067ed5d40e
2 changed files with 44 additions and 1 deletions
+38
View File
@@ -0,0 +1,38 @@
import { ArgumentsHost, HttpException, HttpStatus, Type } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import { isObject } from 'lodash';
import { EntityNotFoundError, EntityPropertyNotFoundError, QueryFailedError } from 'typeorm';
export class AppFilter<T = Error> extends BaseExceptionFilter<T> {
protected resExceptions: Array<{ class: Type<Error>; status?: number } | Type<Error>> = [
{ class: EntityNotFoundError, status: HttpStatus.NOT_FOUND },
{ class: QueryFailedError, status: HttpStatus.BAD_REQUEST },
{ class: EntityPropertyNotFoundError, status: HttpStatus.BAD_REQUEST },
];
// eslint-disable-next-line consistent-return
catch(exception: T, host: ArgumentsHost) {
const applicationRef =
this.applicationRef || (this.httpAdapterHost && this.httpAdapterHost.httpAdapter)!;
const resException = this.resExceptions.find((item) =>
'class' in item ? exception instanceof item.class : exception instanceof item,
);
if (!resException && !(exception instanceof HttpException)) {
return this.handleUnknownError(exception, host, applicationRef);
}
let res: string | object = '';
let status = HttpStatus.INTERNAL_SERVER_ERROR;
if (exception instanceof HttpException) {
res = exception.getResponse();
status = exception.getStatus();
} else if (resException) {
const e = exception as unknown as Error;
res = e.message;
if ('class' in resException && resException.status) {
status = resException.status;
}
}
const message = isObject(res) ? res : { statusCode: status, message: res };
applicationRef!.reply(host.getArgByIndex(1), message, status);
}
}