Nodejs Nestjs 路程 之 异常过滤器Exceptionfilter

自定义异常过滤器处理
由于内置异常处理返回值格式无法调整,因此自定义异常就显得又为正常。自定义异常可以使返回异常信息自定义,且可以增加自定义异常编码,方便客户端人员根据异常编码进行不同的展示。

如何自定义异常?

不重复造轮子是程序员的自我约束,首先我们新建我们自己的异常基类:

复制代码
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}
12 /
13 export abstract class BaseExceptionFilter implements ExceptionFilter
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();
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();
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 }
复制代码
USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com

你可能感兴趣的:(Nodejs Nestjs 路程 之 异常过滤器Exceptionfilter)