zhulj 9c05da023f
2022-06-08 16:47:41 +08:00

32 lines
634 B
JavaScript

const responseHandler = async (ctx, next) => {
ctx.res.success = ({ status, data, msg = 'success' } = {}) => {
ctx.status = status || 200
ctx.body = {
status: ctx.status,
data,
msg
}
}
ctx.res.fail = ({ status, msg = 'fail', data = {} } = {}) => {
ctx.status = status || 400
ctx.body = {
status,
data,
msg
}
}
try {
await next()
} catch (err) {
console.log('中间件错误:', err)
if (err.status)
ctx.res.fail({ status: err.status, msg: err.message })
else
ctx.app.emit('error', err, ctx)
}
}
module.exports = responseHandler