2 回答

TA貢獻1893條經驗 獲得超10個贊
要處理不同類型的 TypeOrm 錯誤,如果異常構造函數匹配任何 TypeOrm 錯誤(來自 node_modules\typeorm\error),您可以切換/ case。此外, (exception as any).code 將提供發生的實際數據庫錯誤。注意 @catch() 裝飾器是空的,以便捕獲所有錯誤類型。
import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus, Logger } from '@nestjs/common';
import { Request, Response } from 'express';
import { QueryFailedError, EntityNotFoundError, CannotCreateEntityIdMapError } from 'typeorm';
import { GlobalResponseError } from './global.response.error';
@Catch()
export class GlobalExceptionFilter implements ExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
let message = (exception as any).message.message;
let code = 'HttpException';
Logger.error(message, (exception as any).stack, `${request.method} ${request.url}`);
let status = HttpStatus.INTERNAL_SERVER_ERROR;
switch (exception.constructor) {
case HttpException:
status = (exception as HttpException).getStatus();
break;
case QueryFailedError: // this is a TypeOrm error
status = HttpStatus.UNPROCESSABLE_ENTITY
message = (exception as QueryFailedError).message;
code = (exception as any).code;
break;
case EntityNotFoundError: // this is another TypeOrm error
status = HttpStatus.UNPROCESSABLE_ENTITY
message = (exception as EntityNotFoundError).message;
code = (exception as any).code;
break;
case CannotCreateEntityIdMapError: // and another
status = HttpStatus.UNPROCESSABLE_ENTITY
message = (exception as CannotCreateEntityIdMapError).message;
code = (exception as any).code;
break;
default:
status = HttpStatus.INTERNAL_SERVER_ERROR
}
response.status(status).json(GlobalResponseError(status, message, code, request));
}
}
import { Request } from 'express';
import { IResponseError } from './response.error.interface';
export const GlobalResponseError: (statusCode: number, message: string, code: string, request: Request) => IResponseError = (
statusCode: number,
message: string,
code: string,
request: Request
): IResponseError => {
return {
statusCode: statusCode,
message,
code,
timestamp: new Date().toISOString(),
path: request.url,
method: request.method
};
};
export interface IResponseError {
statusCode: number;
message: string;
code: string;
timestamp: string;
path: string;
method: string;
}
添加回答
舉報