如何使用 Fastify NestJs 处理异常过滤器中的错误?

问题描述 投票:0回答:2

我使用以下代码来捕获 fastify 中的错误,但是我的错误是“response.send”不是一个函数:

在 Fastify 中使用该结构在全局异常过滤器上发送错误的正确方法是什么?


    import {
      ArgumentsHost,
      Catch,
      ExceptionFilter,
      HttpException,
      HttpStatus,
      Injectable,
    } from '@nestjs/common';
    import dotenv from 'dotenv';
    import { FastifyReply, FastifyRequest } from 'fastify';
    
    
    @Catch()
    @Injectable()
    export class HttpExceptionFilter implements ExceptionFilter {
     
      catch(exception: any, host: ArgumentsHost) {
        const context = host.switchToHttp();
        const response: FastifyReply<any> = context.getResponse<FastifyReply>();
        const request: FastifyRequest = context.getRequest<FastifyRequest>();
        let status =
          exception instanceof HttpException
            ? exception.getStatus()
            : HttpStatus.INTERNAL_SERVER_ERROR;
    
        const message =
          exception instanceof Error ? exception.message : exception.message.error;
    
        response.send({"test":"test"})
      }
    
    }

exception nestjs
2个回答
0
投票

您应该在 catch 装饰器中添加异常,即

@Catch(HttpException)
,并且不使用可注入装饰器。我的自定义 http-exception-filter 代码片段如下所示,并且有效。希望这有帮助。

@Catch(HttpException)
export class HttpExceptionFilter<T extends HttpException> implements ExceptionFilter
    {
      catch(exception: T, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const response: FastifyReply<any> = ctx.getResponse<FastifyReply>();
    
        const status = exception.getStatus();
        const exceptionResponse = exception.getResponse();
    
        const error =
          typeof response === 'string'
            ? { message: exceptionResponse }
            : (exceptionResponse as object);
    
        response
          .status(status)
          .send({ ...error, timestamp: new Date().toISOString() });
      }
    }

0
投票

这对我的FASTIFY有用,您需要添加

constructor
并使用
this.httpAdaptorHost.httpAdaptor.reply
而不是
response.send

@Catch(HttpException)
export default class InternalServerErrorExceptionFilter implements ExceptionFilter {
  constructor(private httpAdapterHost: HttpAdapterHost) {} // Use This

  catch(exception: Error, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<FastifyReply>();

    if (exception instanceof HttpException) {
      const statusCode = exception.getStatus();
      const responseBody = exception.getResponse();

      // LIKE THIS
      return this.httpAdapterHost.httpAdapter.reply(response, responseBody, statusCode);
    }

    // CUSTOM HANDLING, Related to My APP.

    console.error(exception.message);

    const statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
    const responseBody = new ErrorResponseBody(ERROR_MESSAGE.INTERNAL_SERVER_ERROR, 500);

    // other error to re-write InternalServerErrorException response
    return this.httpAdapterHost.httpAdapter.reply(response, responseBody, statusCode);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.