错误响应格式器:把任意错误归一化为 { status, body } 结构。
用户在 app 级错误中间件中调用(三框架一行替换),实现跨框架统一的错误响应体。 不由 adapter 内置——adapter 只做桥接,错误响应格式由用户在 app 级决定。
// Expressapp.use((err, _req, res, _next) => { const { status, body } = defaultErrorFormatter(err); res.status(status).json(body);});// Koaapp.use(async (ctx, next) => { try { await next(); } catch (err) { const { status, body } = defaultErrorFormatter(err); ctx.status = status; ctx.body = body; }}); Copy
// Expressapp.use((err, _req, res, _next) => { const { status, body } = defaultErrorFormatter(err); res.status(status).json(body);});// Koaapp.use(async (ctx, next) => { try { await next(); } catch (err) { const { status, body } = defaultErrorFormatter(err); ctx.status = status; ctx.body = body; }});
错误响应格式器:把任意错误归一化为 { status, body } 结构。
用户在 app 级错误中间件中调用(三框架一行替换),实现跨框架统一的错误响应体。 不由 adapter 内置——adapter 只做桥接,错误响应格式由用户在 app 级决定。
Example