教程 > nest.js 中文教程 > 阅读:513

nest.js 异常过滤器——迹忆客-ag捕鱼王app官网

nest 带有一个内置的异常层,负责处理应用程序中所有未处理的异常。 当我们的应用程序代码未处理异常时,该层将捕获该异常,然后自动发送适当的用户友好响应。

nest.js 异常过滤器
nest.js 异常过滤器

开箱即用,此操作由内置的全局异常过滤器执行,该过滤器处理 httpexception 类型的异常(及其子类)。 当异常无法识别(既不是 httpexception 也不是继承自 httpexception 的类)时,内置异常过滤器会生成以下默认 json 响应:

{
  "statuscode": 500,
  "message": "internal server error"
}

提示 :全局异常过滤器部分支持 http-errors 库。 基本上,任何包含 statuscode 和 message 属性的抛出异常都将被正确填充并作为响应发送回客户端(而不是默认的 internalservererrorexception 用于无法识别的异常)。

抛出标准异常

nest 提供了一个内置的 httpexception 类,是 @nestjs/common 包中的一个公共类。 对于典型的基于 http rest/graphql api 的应用程序,最好在发生某些错误情况时发送标准 http 响应对象。

例如,在 catscontroller 中,我们有一个 findall() 方法(一个 get 路由处理程序)。 让我们假设这个路由处理程序由于某种原因抛出了一个异常。 为了证明这一点,我们将对其进行硬编码,如下所示:

cats.controller.ts

@get()
async findall() {
  throw new httpexception('forbidden', httpstatus.forbidden);
}

提示 : 我们在这里使用了 httpstatus。 这是从 @nestjs/common 包导入的。

当客户端调用此端点时,响应如下所示:

{
  "statuscode": 403,
  "message": "forbidden"
}

httpexception 构造函数接受两个确定响应的必需参数:

  • response : 参数定义 json 响应正文。 它可以是字符串或对象,如下所述。
  • status : 参数定义 http 状态代码。

默认情况下,json response 正文包含两个属性:

  • statuscode :默认为 status 参数中提供的 http 状态码
  • message :基于状态的 http 错误的简短描述

要仅覆盖 json 响应正文的消息部分,请在 response 参数中提供一个字符串。 要覆盖整个 json 响应正文,需要在 response 参数中传递一个对象。 nest 将序列化对象并将其作为 json 响应正文返回。

第二个构造函数参数 - status - 应该是一个有效的 http 状态代码。 最佳实践是使用从 @nestjs/common 包导入的 httpstatus 进行枚举。

这是一个覆盖整个 response 正文的示例:

cats.controller.ts

@get()
async findall() {
  throw new httpexception({
    status: httpstatus.forbidden,
    error: 'this is a custom message',
  }, httpstatus.forbidden);
}

使用上述内容,这就是响应的内容:

{
  "status": 403,
  "error": "this is a custom message"
}

自定义异常

在很多情况下,我们不需要编写自定义异常,并且可以使用内置的 nest http 异常,如下一节所述。 如果确实需要创建自定义异常,最好创建自己的异常层次结构,其中自定义异常继承自基类 httpexception 。 通过这种方法,nest 将识别我们的异常,并自动处理错误响应。 让我们实现这样一个自定义异常:

forbidden.exception.ts

export class forbiddenexception extends httpexception {
  constructor() {
    super('forbidden', httpstatus.forbidden);
  }
}

由于 forbiddenexception 继承了基类 httpexception,它将与内置的异常处理程序无缝协作,因此我们可以在 findall() 方法中使用它。

cats.controller.ts

@get()
async findall() {
  throw new forbiddenexception();
}

内置的 http 异常

nest 提供了一组从基础 httpexception 继承的标准异常。 这些异常都是 @nestjs/common 包中的公共类,代表了许多最常见的 http 异常:

  • badrequestexception
  • unauthorizedexception
  • notfoundexception
  • forbiddenexception
  • notacceptableexception
  • requesttimeoutexception
  • conflictexception
  • goneexception
  • httpversionnotsupportedexception
  • payloadtoolargeexception
  • unsupportedmediatypeexception
  • unprocessableentityexception
  • internalservererrorexception
  • notimplementedexception
  • imateapotexception
  • methodnotallowedexception
  • badgatewayexception
  • serviceunavailableexception
  • gatewaytimeoutexception
  • preconditionfailedexception

异常过滤器

虽然基本(内置)异常过滤器可以为我们自动处理许多情况,但我们可能希望完全控制异常层。 例如,可能想要添加日志记录或基于某些动态因素使用不同的 json 模式。 异常过滤器正是为此目的而设计的。 它们使我们可以控制确切的控制流以及发送回客户端的响应内容。

让我们创建一个异常过滤器,负责捕获作为 httpexception 类实例的异常,并为它们实现自定义响应逻辑。 为此,我们需要访问底层平台的 request 和 response 对象。 我们将访问 request 对象,以便提取原始 url 并将其包含在日志信息中。 我们将使用 response 对象直接控制发送的响应,使用 response.json() 方法。

http-exception.filter.ts

import { exceptionfilter, catch, argumentshost, httpexception } from '@nestjs/common';
import { request, response } from 'express';
@catch(httpexception)
export class httpexceptionfilter implements exceptionfilter {
  catch(exception: httpexception, host: argumentshost) {
    const ctx = host.switchtohttp();
    const response = ctx.getresponse();
    const request = ctx.getrequest();
    const status = exception.getstatus();
    response
      .status(status)
      .json({
        statuscode: status,
        timestamp: new date().toisostring(),
        path: request.url,
      });
  }
}

提示 : 所有异常过滤器都应该实现通用的 exceptionfilter 接口。 这要求我们提供带有指定签名的 catch(exception: t, host: argumentshost) 方法。 t 表示异常的类型。

@catch(httpexception) 装饰器将所需的元数据绑定到异常过滤器,告诉 nest 这个特定的过滤器正在寻找 httpexception 类型的异常,而不是别的。 @catch() 装饰器可以采用单个参数或逗号分隔的列表。 这使我们可以同时为多种类型的异常设置过滤器。

让我们看一下 catch() 方法的参数。 exception 参数是当前正在处理的异常对象。 host 参数是一个 argumentshost 对象。 argumentshost 是一个功能强大的实用程序对象,我们将在执行上下文一章中进一步研究它。在此代码示例中,我们使用它来获取对传递给原始请求处理程序(在异常产生的控制器中)的 requestresponse 对象的引用。并且我们在 argumentshost 上使用了一些辅助方法来获取所需的 request 和 response 对象。

这种抽象级别的原因是 argumentshost 在所有上下文中都起作用(例如,我们现在使用的 http 服务器上下文,还有微服务和 websockets)。在执行上下文一章中,我们将看到如何利用 argumentshost 及其辅助函数的强大功能访问任何执行上下文的适当底层参数。这将允许我们编写跨所有上下文操作的通用异常过滤器。


绑定过滤器

让我们将新的 httpexceptionfilter 绑定到 catscontrollercreate() 方法。

cats.controller.ts

@post()
@usefilters(new httpexceptionfilter())
async create(@body() createcatdto: createcatdto) {
  throw new forbiddenexception();
}

提示 : @usefilters() 装饰器是从 @nestjs/common 包中导入的。

我们在这里使用了 @usefilters() 装饰器。 与 @catch() 装饰器类似,它可以采用单个过滤器实例,或以逗号分隔的过滤器实例列表。 在这里,我们创建了 httpexceptionfilter 的实例。 或者,可以传递类(而不是实例),将实例化的责任留给框架,并启用依赖注入。

cats.controller.ts

@post()
@usefilters(httpexceptionfilter)
async create(@body() createcatdto: createcatdto) {
  throw new forbiddenexception();
}

提示 :尽可能使用类而不是实例来应用过滤器。 它减少了内存使用,因为 nest 可以轻松地在整个模块中重用同一类的实例。

在上面的示例中,httpexceptionfilter 仅应用于单个 create() 路由处理程序,使其具有方法范围。 异常过滤器的范围可以分为不同的级别:方法范围、控制器范围或全局范围。 例如,要将过滤器设置为控制器范围,我们可以执行以下操作:

cats.controller.ts

@usefilters(new httpexceptionfilter())
export class catscontroller {}

此构造为 catscontroller 中定义的每个路由处理程序设置 httpexceptionfilter。

要创建全局范围的过滤器,我们将执行以下操作:

main.ts

async function bootstrap() {
  const app = await nestfactory.create(appmodule);
  app.useglobalfilters(new httpexceptionfilter());
  await app.listen(3000);
}
bootstrap();

警告 : useglobalfilters() 方法不会为网关或混合应用程序设置过滤器。

全局范围的过滤器用于整个应用程序,用于每个控制器和每个路由处理程序。 在依赖注入方面,从任何模块外部注册的全局过滤器(使用上面示例中的 useglobalfilters() )不能注入依赖项,因为这是在任何模块的上下文之外完成的。 为了解决这个问题,我们可以使用以下结构直接从任何模块注册一个全局范围的过滤器:

app.module.ts

import { module } from '@nestjs/common';
import { app_filter } from '@nestjs/core';
@module({
  providers: [
    {
      provide: app_filter,
      useclass: httpexceptionfilter,
    },
  ],
})
export class appmodule {}

当使用这种方法为过滤器执行依赖注入时,请注意,无论使用此构造的模型是什么情况,过滤器实际上都是全局的。 这应该在哪里完成? 选择定义过滤器(上例中的 httpexceptionfilter)的模型。 此外,useclass 不是处理自定义提供程序注册的唯一方法。

我们可以根据需要使用此技术添加任意数量的过滤器; 只需将每个过滤器添加到提供程序数组。


捕获任意异常

为了捕获每个未处理的异常(无论异常类型如何),请将 @catch() 装饰器的参数列表留空,例如 **@catch()**。

在下面的示例中,我们有一个与平台无关的代码,因为它使用 http 适配器来传递响应,并且不直接使用任何特定于平台的对象( request 和 response ):

import {
  exceptionfilter,
  catch,
  argumentshost,
  httpexception,
  httpstatus,
} from '@nestjs/common';
import { httpadapterhost } from '@nestjs/core';
@catch()
export class allexceptionsfilter implements exceptionfilter {
  constructor(private readonly httpadapterhost: httpadapterhost) {}
  catch(exception: unknown, host: argumentshost): void {
    // in certain situations `httpadapter` might not be available in the
    // constructor method, thus we should resolve it here.
    const { httpadapter } = this.httpadapterhost;
    const ctx = host.switchtohttp();
    const httpstatus =
      exception instanceof httpexception
        ? exception.getstatus()
        : httpstatus.internal_server_error;
    const responsebody = {
      statuscode: httpstatus,
      timestamp: new date().toisostring(),
      path: httpadapter.getrequest),
    };
    httpadapter.reply(ctx.getresponse(), responsebody, httpstatus);
  }
}

继承

通常,我们将创建完全定制的异常过滤器,以满足自己的应用程序要求。 但是,当想简单地继承内置的默认全局异常过滤器并根据某些因素覆盖行为时,可能会有用例。

为了将异常处理委托给基本过滤器,我们需要扩展 baseexceptionfilter 并调用继承的 catch() 方法。

all-exceptions.filter.ts

import { catch, argumentshost } from '@nestjs/common';
import { baseexceptionfilter } from '@nestjs/core';
@catch()
export class allexceptionsfilter extends baseexceptionfilter {
  catch(exception: unknown, host: argumentshost) {
    super.catch(exception, host);
  }
}

警告 : 扩展 baseexceptionfilter 的方法范围和控制器范围的过滤器不应使用 new 实例化。 相反,让框架自动实例化它们。

上面的实现只是一个演示该方法的外壳。 我们对继承异常过滤器的实现将包括定制的业务逻辑(例如,处理各种条件)。

全局过滤器可以继承基本过滤器。 这可以通过两种方式中的任何一种来完成。

第一种方法是在实例化自定义全局过滤器时注入 httpadapter 引用:

async function bootstrap() {
  const app = await nestfactory.create(appmodule);
  const { httpadapter } = app.get(httpadapterhost);
  app.useglobalfilters(new allexceptionsfilter(httpadapter));
  await app.listen(3000);
}
bootstrap();

第二种方法是使用 app_filter 令牌。

查看笔记

扫码一下
查看教程更方便
网站地图