import { Catch, Inject, MidwayEnvironmentService } from '@midwayjs/core'; import { Context } from '@midwayjs/koa'; import { ServiceError } from '../error/CustomerError.error'; import { pick } from 'lodash'; const pickList = ['errmsg', 'errcode']; const devList = ['name', 'stack']; // 指定那些异常来这里处理 @Catch([ServiceError]) export class CustomErrorFilter { @Inject() ctx: Context; @Inject() envService: MidwayEnvironmentService; async catch(err: Error, ctx: Context) { const pl = pickList; const is_dev = this.envService.isDevelopmentEnvironment; if (is_dev) { pl.push(...devList); } const obj = pick(err, pl); const result: any = obj; return result; } }