| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2022-01-20 16:05:23 |
| 4 | * @LastEditors: nevin |
| 5 | * @LastEditTime: 2025-04-27 14:35:40 |
| 6 | * @Description: 全局错误处理 |
| 7 | */ |
| 8 | import { |
| 9 | ArgumentsHost, |
| 10 | Catch, |
| 11 | ExceptionFilter, |
| 12 | HttpException, |
| 13 | UnauthorizedException, |
| 14 | HttpStatus, |
| 15 | Logger, |
| 16 | BadRequestException, |
| 17 | } from '@nestjs/common'; |
| 18 | import { HttpResult } from '../global/interface/response.interface'; |
| 19 | |
| 20 | import { ErrHttpBackMap } from './http-exception.back-code'; |
| 21 | |
| 22 | const BASE_ERROR_CODE = '1'; |
| 23 | const ARG_ERROR_CODE = '-1'; // 参数错误码 |
| 24 | |
| 25 | interface ExceptionResponseObj { |
| 26 | message?: ''; |
| 27 | statusCode?: ''; |
| 28 | error?: ''; |
| 29 | } |
| 30 | |
| 31 | // 全部错误 |
| 32 | @Catch(Error) |
| 33 | export class AppExceptionFilter implements ExceptionFilter { |
| 34 | catch(error: Error, host: ArgumentsHost) { |
| 35 | const ctx = host.switchToHttp(); |
| 36 | const response = ctx.getResponse(); |
| 37 | const request = ctx.getRequest(); |
| 38 | console.log('---- Error ---', error); |
| 39 | |
| 40 | Logger.error({ |
| 41 | url: request.originalUrl, |
| 42 | level: 'error', |
| 43 | message: request.originalUrl, |
| 44 | mate: error.stack, |
| 45 | stack: error.stack, |
| 46 | }); |
| 47 | |
| 48 | const errorResponse: HttpResult<string> = { |
| 49 | data: '', |
| 50 | msg: '', |
| 51 | code: BASE_ERROR_CODE, // 自定义code |
| 52 | url: request.originalUrl, // 错误的url地址 |
| 53 | }; |
| 54 | |
| 55 | response.status(HttpStatus.INTERNAL_SERVER_ERROR); |
| 56 | response.header('Content-Type', 'application/json; charset=utf-8'); |
| 57 | response.send(errorResponse); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | @Catch(BadRequestException) |
| 62 | export class BadExceptionFilter implements ExceptionFilter { |
| 63 | catch(error: Error, host: ArgumentsHost) { |
| 64 | const ctx = host.switchToHttp(); |
| 65 | const response = ctx.getResponse(); |
| 66 | const request = ctx.getRequest(); |
| 67 | Logger.error({ |
| 68 | url: request.originalUrl, |
| 69 | level: 'error', |
| 70 | message: request.originalUrl, |
| 71 | mate: error.stack, |
| 72 | stack: error.stack, |
| 73 | }); |
| 74 | |
| 75 | const errorResponse: HttpResult<string> = { |
| 76 | data: '', |
| 77 | msg: error.message, |
| 78 | code: ARG_ERROR_CODE, // 自定义code |
| 79 | url: request.originalUrl, // 错误的url地址 |
| 80 | }; |
| 81 | |
| 82 | response.status(HttpStatus.INTERNAL_SERVER_ERROR); |
| 83 | response.header('Content-Type', 'application/json; charset=utf-8'); |
| 84 | response.send(errorResponse); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // 认证错误 |
| 89 | @Catch(UnauthorizedException) |
| 90 | export class AuthExceptionFilter implements ExceptionFilter { |
| 91 | catch(error: Error, host: ArgumentsHost) { |
| 92 | const ctx = host.switchToHttp(); |
| 93 | const response = ctx.getResponse(); |
| 94 | const request = ctx.getRequest(); |
| 95 | |
| 96 | Logger.error({ |
| 97 | url: request.originalUrl, |
| 98 | level: 'error', |
| 99 | message: request.originalUrl, |
| 100 | mate: error.stack, |
| 101 | stack: error.stack, |
| 102 | }); |
| 103 | |
| 104 | const errorResponse: HttpResult<string> = { |
| 105 | data: '', |
| 106 | msg: '', |
| 107 | code: BASE_ERROR_CODE, // 自定义code |
| 108 | url: request.originalUrl, // 错误的url地址 |
| 109 | }; |
| 110 | |
| 111 | response.status(HttpStatus.UNAUTHORIZED); |
| 112 | response.header('Content-Type', 'application/json; charset=utf-8'); |
| 113 | response.send(errorResponse); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // 业务报错 |
| 118 | export class AppHttpException extends HttpException { |
| 119 | constructor(errKey: string) { |
| 120 | super(errKey, HttpStatus.OK); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | @Catch(HttpException) |
| 125 | export class HttpExceptionFilter implements ExceptionFilter { |
| 126 | catch(exception: HttpException, host: ArgumentsHost) { |
| 127 | const ctx = host.switchToHttp(); |
| 128 | const response = ctx.getResponse(); |
| 129 | const request = ctx.getRequest(); |
| 130 | |
| 131 | // 定义错误的返回对象 |
| 132 | const errorResponse: HttpResult<string | object> = { |
| 133 | data: exception.getResponse(), |
| 134 | msg: '', |
| 135 | code: BASE_ERROR_CODE, // 自定义code |
| 136 | url: request.originalUrl, // 错误的url地址 |
| 137 | }; |
| 138 | |
| 139 | const defErrHttpBack = ErrHttpBackMap.get('fail'); |
| 140 | const errObj = exception.getResponse(); // 获取的错误返回对象 |
| 141 | |
| 142 | if (typeof errObj === 'object') { |
| 143 | errorResponse.msg = |
| 144 | (<ExceptionResponseObj>errObj).message || defErrHttpBack.message; |
| 145 | errorResponse.code = |
| 146 | (<ExceptionResponseObj>errObj).error || defErrHttpBack.errCode; |
| 147 | errorResponse.data = errObj; |
| 148 | } |
| 149 | |
| 150 | if (typeof errObj === 'string') { |
| 151 | const errBackObj = |
| 152 | ErrHttpBackMap.get(exception.message) || defErrHttpBack; |
| 153 | |
| 154 | errorResponse.code = errBackObj.errCode; |
| 155 | errorResponse.msg = errBackObj.message || errObj; |
| 156 | errorResponse.data = ''; |
| 157 | } |
| 158 | |
| 159 | Logger.log(errorResponse.code + ':' + errorResponse.msg); |
| 160 | |
| 161 | // 设置返回的状态码、请求头、发送错误信息 |
| 162 | response.status( |
| 163 | exception instanceof HttpException |
| 164 | ? exception.getStatus() |
| 165 | : HttpStatus.INTERNAL_SERVER_ERROR, |
| 166 | ); |
| 167 | response.header('Content-Type', 'application/json; charset=utf-8'); |
| 168 | response.send(errorResponse); |
| 169 | } |
| 170 | } |
| 171 |