Newer
Older
const HttpError = require('core/errors/HttpError')

Louis
committed
const SafeModeError = require('core/errors/SafeModeError')
const SentryReporter = require('./SentryReporter')
module.exports = async (ctx, next) => {
let hasHandledError = false
try {
await next(ctx)
} catch (e) {

Louis
committed
if (e instanceof SafeModeError) {
console.error(e)
} else {
await SentryReporter.report(e, ctx)
}
hasHandledError = true
if (e instanceof HttpError) {
e.respondTo(ctx)
} else {
const message = e.message
ctx.status = 500
ctx.body = {
errors: {
general: [message],
},
}
}
}
if (ctx.status >= 400 && !hasHandledError) {
await SentryReporter.reportHttp(ctx)
}
}