[{"data":1,"prerenderedAt":85},["ShallowReactive",2],{"content:\u002Fposts\u002F2022\u002Fnest-exceptionfilter":3,"surround:\u002Fposts\u002F2022\u002Fnest-exceptionfilter":74},{"id":4,"title":5,"body":6,"canonical":50,"categories":51,"date":53,"description":54,"draft":55,"extension":56,"image":57,"meta":58,"navigation":59,"path":60,"permalink":50,"readingTime":61,"recommend":50,"references":50,"seo":66,"sitemap":67,"stem":68,"tags":69,"type":50,"updated":53,"__hash__":73},"content\u002Fposts\u002F2022\u002Fnest-ExceptionFilter.md","nest 拦截器",{"type":7,"value":8,"toc":43},"minimark",[9,13,24,28,34,37],[10,11,12],"h2",{"id":12},"全局响应拦截器",[14,15,21],"pre",{"className":16,"code":18,"language":19,"meta":20},[17],"language-ts","import { ArgumentsHost, Catch, ExceptionFilter, HttpStatus, ServiceUnavailableException } from '@nestjs\u002Fcommon'\nimport { FastifyReply, FastifyRequest } from 'fastify'\n\n@Catch()\nexport class AllExceptionsFilter implements ExceptionFilter {\n    catch(exception: Error, host: ArgumentsHost) {\n        const ctx = host.switchToHttp()\n        const response = ctx.getResponse\u003CFastifyReply>()\n        const request = ctx.getRequest\u003CFastifyRequest>()\n\n        request.log.error(exception)\n\n        \u002F\u002F 非 HTTP 标准异常处理\n        response.status(HttpStatus.SERVICE_UNAVAILABLE).send({\n            statusCode: HttpStatus.SERVICE_UNAVAILABLE,\n            timestamp: new Date().toISOString(),\n            path: request.url,\n            message: new ServiceUnavailableException().getResponse(),\n        })\n    }\n}\n","ts","",[22,23,18],"code",{"__ignoreMap":20},[10,25,27],{"id":26},"全局-http-响应拦截器","全局 HTTP 响应拦截器",[14,29,32],{"className":30,"code":31,"language":19,"meta":20},[17],"import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus } from '@nestjs\u002Fcommon'\nimport { FastifyReply, FastifyRequest } from 'fastify'\n\nimport { BusinessExceptions } from '.\u002Fbusiness.exception.filter'\n\n@Catch(HttpException)\nexport class HttpExceptionsFilter implements ExceptionFilter {\n    catch(exception: HttpException, host: ArgumentsHost) {\n        const ctx = host.switchToHttp()\n        const response = ctx.getResponse\u003CFastifyReply>()\n        const request = ctx.getRequest\u003CFastifyRequest>()\n        const status = exception.getStatus()\n\n        if (exception instanceof BusinessExceptions) {\n            const error = exception.getResponse()\n            response.status(HttpStatus.OK).send({\n                data: null,\n                status: error.code,\n                extra: {},\n                message: error.message,\n                success: false,\n            })\n            return\n        }\n        response.status(status).send({\n            statusCode: status,\n            timestamp: new Date().toISOString(),\n            path: request.url,\n            message: exception.getResponse(),\n        })\n    }\n}\n",[22,33,31],{"__ignoreMap":20},[10,35,36],{"id":36},"自定义拦截器",[14,38,41],{"className":39,"code":40,"language":19,"meta":20},[17],"import { HttpException, HttpStatus } from '@nestjs\u002Fcommon'\n\nimport { BUSINESS_ERROR_CODE } from '.\u002Fbusiness.error.codes'\n\ninterface BusinessError {\n    code: number\n    message: string\n}\n\nexport class BusinessExceptions extends HttpException {\n    constructor(err: BusinessError | string) {\n        if (typeof err === 'string') {\n            err = {\n                code: BUSINESS_ERROR_CODE.COMMON,\n                message: err,\n            }\n        }\n        super(err, HttpStatus.OK)\n    }\n\n    static throwForbidden() {\n        throw new BusinessExceptions({\n            code: BUSINESS_ERROR_CODE.ACCESS_FORBIDDEN,\n            message: '抱歉哦，您无此权限！',\n        })\n    }\n}\n",[22,42,40],{"__ignoreMap":20},{"title":20,"searchDepth":44,"depth":44,"links":45},4,[46,48,49],{"id":12,"depth":47,"text":12},2,{"id":26,"depth":47,"text":27},{"id":36,"depth":47,"text":36},null,[52],"代码","2022-12-04 16:14:54","实现几个常用的 nest 拦截器",false,"md","https:\u002F\u002Fbitmc.uno\u002Fpicgo\u002F67a2262d9a02e-78.webp",{},true,"\u002Fposts\u002F2022\u002Fnest-exceptionfilter",{"text":62,"minutes":63,"time":64,"words":65},"2 min read",1.205,72300,241,{"title":5,"description":54},{"loc":60},"posts\u002F2022\u002Fnest-ExceptionFilter",[70,71,72],"node","nest","服务端","Nn5Dry3IdNQ_dHCexgUJkTzCYTLs0x9_ty0TE_wAXvs",[75,80],{"title":76,"path":77,"stem":78,"date":79,"type":50,"children":-1},"MIME 类型和 Blob","\u002Fposts\u002F2022\u002Fblob-and-mime","posts\u002F2022\u002Fblob-and-mime","2022-12-04 16:12:29",{"title":81,"path":82,"stem":83,"date":84,"type":50,"children":-1},"nest 服务提供者","\u002Fposts\u002F2022\u002Fnest-providers","posts\u002F2022\u002Fnest-providers","2022-12-04 16:15:46",1789642595984]