Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save corysimmons/310c7da0d163c90328687f35b95e548b to your computer and use it in GitHub Desktop.
Save corysimmons/310c7da0d163c90328687f35b95e548b to your computer and use it in GitHub Desktop.
todo
app.use(async (ctx, next) => {
try {
await next()
ctx.body = normalizeInterface(ctx)
} catch (err) {
ctx.body = normalizeInterface(ctx, err)
}
})
app.use(async (ctx) => {
// Throw a multi error
if (stuff) {
throw new MultiError('Shit dawg')
.add('MYAPP_001', 'Email in use')
.add('MYAPP_002', 'Username in use')
}
})
// Required to extend an error.
const ExtendableError = require('es6-error')
class MultiError extends ExtendableError {
errors = []
add (code, text) {
this.errors.push({ code, text })
return this
}
}
function normalizeInterface (ctx, err) {
if (err && err instanceof MultiError) {
return {
status_code: ctx.status,
error_details: err.errors
}
}
return {
status_code: ctx.status,
content: ctx.body
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment