import { Middleware } from '@midwayjs/core'; import { Context, NextFunction } from '@midwayjs/koa'; @Middleware() export class ErrorMiddleware { static getName(): string { return 'error'; } resolve() { return async (ctx: Context, next: NextFunction) => { try { await next(); } catch (err: any) { console.error('全局错误捕获:', err); const status = err.status || 500; const message = err.message || '服务器内部错误'; ctx.status = status; ctx.body = { code: status, message: message, data: null, }; } }; } ignore(ctx: Context): boolean { return false; } }