Nestjs 路程 之 异常过滤器Exceptionfilter
参考文档:docs.nestjs.cn
说起Nestjs的异常过滤器,不能不提.Net的全局过滤器Filter,功能那是相当的强悍,用理论话说叫AOP 面向切面编程,可谓方便了太多需要异常处理的场景。说回Nestjs的异常过滤器,实现类似的功能,采用相似的处理方式,只不过一个面向C#,一个面向Nodejs,很荣幸的我,在两个框架都找到了类似的东西。
面向切面编程AOP,是一种类似于编程规范的东东,同门师兄弟有叫面向接口编程、SOLID原则等等。
Nestjs的异常处理
默认异常处理
Nestjs内置了默认的全局异常过滤器,处理能够转换成Httpexception的异常。
如果是Httpexception或其子类异常,那么会返回该异常的JSON格式:
{"exceptionCode":40005,"message":"自定义异常","path":"/"}
如果不是Httpexception或其子类异常,那么会返回:
{"statusCode":500,"message":"Internal server error"}
由于Nestjs采用了内置的默认异常处理,因此不会出现由于出现未捕获的异常导致程序崩溃。
自定义异常过滤器处理
由于内置异常处理返回值格式无法调整,因此自定义异常就显得又为正常。自定义异常可以使返回异常信息自定义,且可以增加自定义异常编码,方便客户端人员根据异常编码进行不同的展示。
如何自定义异常?
不重复造轮子是程序员的自我约束,首先我们新建我们自己的异常基类:
1 import { HttpException } from "@nestjs/common";
2
3 /**
4 * 定义基础异常类
5 *
6 * @export
7 * @class BaseException
8 * @extends {HttpException}
9 */
10 export class BaseException extends HttpException {
11
12 /**
13 * Creates an instance of BaseException.
14 * @param {number} exceptionCode 自定义异常编号
15 * @param {string} errorMessage 提示信息
16 * @param {number} statusCode 状态码
17 * @memberof BaseException
18 */
19 constructor(public exceptionCode: number, public errorMessage: string, public statusCode: number) {
20 super({ exceptionCode: exceptionCode, errorMessage: errorMessage }, statusCode);
21 }
22
23 /**
24 * 获取自定义异常代码
25 *
26 * @return {*}
27 * @memberof BaseException
28 */
29 getExceptionCode(): number {
30 return this.exceptionCode;
31 }
32
33 getErrorMessage(): string {
34 return this.errorMessage;
35 }
36
37 getStatusCode(): number {
38 return this.statusCode;
39 }
40 }
然后我们新建一个未授权异常类型,其中增加了自定义异常代码:
1 import { HttpStatus } from "@nestjs/common";
2 import { BaseException } from "./base.exception";
3
4 export class UnCauhtException extends BaseException {
5 constructor() {
6 super(40000, "系统运行异常,请联系管理员!", HttpStatus.FORBIDDEN);
7 }
8 }
建立好了自定义异常,那么我们就需要处理未授权异常,首先新建自定义异常处理基类,请注意 此处我们使用的事Express:
1 import { ArgumentsHost, ExceptionFilter, HttpException } from "@nestjs/common";
2 import { HttpArgumentsHost } from "@nestjs/common/interfaces";
3 import { BaseException } from "src/exceptions/base.exception";
4 import { Response, Request } from "express";
5
6 /**
7 * 异常基础类过滤器
8 *
9 * @export
10 * @class BaseExceptionFilter
11 * @implements {ExceptionFilter<BaseException>}
12 */
13 export abstract class BaseExceptionFilter implements ExceptionFilter<BaseException>
14 {
15 /**
16 * 异常类捕获
17 *
18 * @abstract
19 * @param {BaseException} exception
20 * @param {ArgumentsHost} host
21 * @memberof BaseExceptionFilter
22 */
23 abstract catch(exception: BaseException, host: ArgumentsHost);
24
25 /**
26 * 获取http请求上下文参数
27 *
28 * @protected
29 * @param {ArgumentsHost} host
30 * @return {*}
31 * @memberof BaseExceptionFilter
32 */
33 protected getHttpContext(host: ArgumentsHost) {
34 return host.switchToHttp();
35 }
36
37 /**
38 * 获取http 响应参数
39 *
40 * @protected
41 * @param {HttpArgumentsHost} httpContext
42 * @return {*}
43 * @memberof BaseExceptionFilter
44 */
45 protected getResponse(httpContext: HttpArgumentsHost): Response {
46 return httpContext.getResponse<Response>();
47 }
48
49 /**
50 * 获取http请求参数
51 *
52 * @protected
53 * @param {HttpArgumentsHost} httpContext
54 * @return {*}
55 * @memberof BaseExceptionFilter
56 */
57 protected getRequest(httpContext: HttpArgumentsHost): Request {
58 return httpContext.getRequest<Request>();
59 }
60
61 /**
62 * 写入异常信息到客户端
63 *
64 * @param {ArgumentsHost} host
65 * @param {BaseException} exception
66 * @memberof BaseExceptionFilter
67 */
68 protected writeToClient(host: ArgumentsHost, exception: BaseException) {
69 const ctx = this.getHttpContext(host);
70 if(exception instanceof BaseException){
71 this.getResponse(ctx).status(exception.statusCode).json({
72 exceptionCode: exception.getExceptionCode(),
73 message: exception.getErrorMessage(),
74 path: this.getRequest(ctx).url
75 });
76 }else {
77 const httpException=exception ;
78 this.getResponse(ctx).status(500).json({
79 message: "未处理的异常",
80 path: this.getRequest(ctx).url
81 });
82 }
83
84 }
85 }
新建未授权异常处理:
1 import { ArgumentsHost, Catch } from "@nestjs/common";
2 import { AuthException } from "src/exceptions/auth.exception";
3 import { BaseException } from "src/exceptions/base.exception";
4 import { BaseExceptionFilter } from "./base.exception.filter";
5
6 @Catch(AuthException)
7 export class AuthExceptionFilter extends BaseExceptionFilter
8 {
9 constructor(){
10 super();
11 console.log("授权异常构造函数初始化"+new Date().toISOString());
12 }
13 catch(exception: AuthException, host: ArgumentsHost) {
14 exception.exceptionCode=40002;
15 console.log("授权异常执行"+new Date().toISOString());
16 this.writeToClient(host,exception);
17 }
18
19 }
针对未授权异常处理类,进行几点说明:
- 增加了Catch注解,只捕获Authexception的异常,其他类型的异常此类不进行处理
- 继承自定义异常处理类Baseexceptionfilter
应用范围
异常处理类可应用于method、controller、全局,甚至同一个Controller可以定义多个自定义异常类
1 import { Controller, ForbiddenException, Get, HttpException, HttpStatus, UseFilters } from '@nestjs/common';
2 import { AppService } from './app.service';
3 import { AuthException } from './exceptions/auth.exception';
4 import { BusinessException } from './exceptions/business.exception';
5 import { UnCauhtException } from './exceptions/uncauht.exception';
6 import { AuthExceptionFilter } from './filters/auth.exception.filter';
7 import { BusinessExceptionFilter } from './filters/business.exception.filter';
8
9
10 /**
11 * 带有单个路由的基本控制器示例ff
12 */
13 @UseFilters(AuthExceptionFilter,BusinessExceptionFilter)
14 @Controller()
15 export class AppController {
16 constructor(private readonly appService: AppService) {}
17
18 @Get()
19 getHello(): string {
20 //throw new Error("666");
21 throw new BusinessException("自定义异常",HttpStatus.OK);
22 throw new AuthException();
23 throw new HttpException("自定义异常",HttpStatus.FORBIDDEN);
24 return this.appService.getHello();
25 }
26
27 @Get("name")
28 getName():string
29 {
30 return "guozhiqi";
31 }
32 }
几点说明:
- 我们使用Usefilters注解进行异常过滤器的添加
- 我们在Appcontroller中定义了两种不同类型的自定义异常处理类
- 也就是我们Appcontroller中抛出的异常,只要是我们定义的这两种,那么都可以被正常处理。
几点疑问
- Usefitlers中我们自定义的异常处理类会初始化几次?
答案:我们通过类型注册到Appcontroller的自定义异常类只会在程序初始化的时候初始化一次。也就是说程序启动之后,每个controller、每个method定义了哪些异常处理类都已经确定。 - 如果我们捕获到异常,但不进行任何处理,会发生什么?
答案:如果我们的异常处理方法什么也不做,那么恭喜你,会成功的将浏览器请求hang死,因为异常未处理,那么浏览器会一直得不到响应。 - 多个异常之间的处理顺序如何?
答案:如果多个异常处理均可以捕获到该异常,那么只有第一个有效,也就是说异常处理类和 中间件不同,异常处理类只能其中一个处理,而中间件需要都进行处理。 - Nestjs的@Usefilters 像谁?
首先从JS角度来看,像Angular,如果往后端看,最像Spring。
Nestjs的异常处理并不复杂,复杂的是需要我们针对不同类型的异常进行处理,提取异常的共性。
Nestjs 路程 之 异常过滤器Exceptionfilter的更多相关文章
- asp.net core MVC 全局过滤器之ExceptionFilter异常过滤器(一)
本系类将会讲解asp.net core MVC中的内置全局过滤器的使用,将分为以下章节 asp.net core MVC 过滤器之ExceptionFilter异常过滤器(一) asp.net cor ...
- MVC异常日志生产者消费者模式记录(异常过滤器)
生产者消费者模式 定义自己的异常过滤器并注册 namespace Eco.Web.App.Models { public class MyExceptionAttribute : HandleErro ...
- MVC 全局异常过滤器HandleErrorAttribute
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 笨鸟先飞之ASP.NET MVC系列之过滤器(06异常过滤器)
概念介绍 异常过滤器主要在我们方法中出现异常的时候触发,一般我们用 异常过滤器 记录日志,或者在产生异常时做友好的处理 如果我们需要创建异常过滤器需要实现IExceptionFilter接口. nam ...
- MVC教程九:异常过滤器
我们平常在程序里面为了捕获异常,会加上try-catch-finally代码,但是这样会使得程序代码看起来很庞大,在MVC中我们可以使用异常过滤器来捕获程序中的异常,如下图所示: 使用了异常过滤器以后 ...
- [Asp.net MVC]HandleErrorAttribute异常过滤器
摘要 在asp.net mvc中除了使用try...catch/finally来处理异常外,它提供了一种通过在Controller或者Action上添加特性的方式来处理异常. HandleErrorA ...
- MVC与WebApi中的异常过滤器
一.MVC的异常过滤器 1.自定义MVC异常过滤器 创建一个类,继承HandleErrorAttribute即可,如果不需要作为特性使用直接实现IExceptionFilter接口即可, 注意,该 ...
- MVC异常过滤器 (错误页)
控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...
- MVC异常过滤器
MVC过滤器 一般的过滤器执行顺序 IAuthorizationFilter->OnAuthorization(授权) IActionFilter ->OnActionE ...
随机推荐
- MariaDB Galera Cluster集群搭建
MariaDB Galera Cluster是什么? Galera Cluster是由第三方公司Codership所研发的一套免费开源的集群高可用方案,实现了数据零丢失,官网地址为http://g ...
- Let’s Encrypt 通配符证书,泛域名证书申请配置
首先你可以查看下官方提供的支持申请通配符证书的客户端列表:https://letsencrypt.org/docs/client-options/. 参考链接:https://github.com/N ...
- Study_way
一.Study 学习通Java基础视频.语法 开源中国 (Git)版本控制 读懂程序.源代码 相关资源 百度网盘 程序:方法(数学) 二.参数传递 基本数据的传参:虚参改变影响实参 引用数据的传参:数 ...
- Spring Boot 2.x基础教程:使用Flyway管理数据库版本
之前已经介绍了很多在Spring Boot中使用MySQL的案例,包含了Spring Boot最原始的JdbcTemplate.Spring Data JPA以及我们国内最常用的MyBatis.同时, ...
- 【老孟Flutter】为什么 build 方法放在 State 中而不是在 StatefulWidget 中
老孟导读:此篇文章是生命周期相关文章的番外篇,在查看源码的过程中发现了这一有趣的问题,欢迎大家一起探讨. Flutter 中Stateful 组件的生命周期:http://laomengit.com/ ...
- LeetCode876 链表的中间结点
给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 1: 输入:[1,2,3,4,5] 输出:此列表中的结点 3 (序列化形式:[3,4 ...
- LeetCode24 两两交换链表中的节点
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...
- 简单的JS+CSS实现网页自定义换肤
1,实现效果 2,实现原理 主要原理是利用css变量设置颜色,用js动态修改变量,使颜色变化,兼容性如下: 实现换肤之前先要了解一下伪类选择器 :root ,还有css的 var() 函数和 set ...
- 【Spring】Spring中的Bean - 2、Baen的实例化 (构造器、静态工厂、实例工厂)
Bean的实例化 文章目录 Bean的实例化 构造器实例化 静态工厂方式实例化 实例工厂方式实例化 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-S ...
- CTFhub刷题记录
一 [WesternCTF2018]shrine 没什么好说的,SSTI模版注入类问题,过滤了()但是我们不慌.开始注入,{{29*3}}测试通过. 发现是jinjia2的模版注入.关键点在于没有() ...