Created
August 24, 2017 07:54
-
-
Save corysimmons/310c7da0d163c90328687f35b95e548b to your computer and use it in GitHub Desktop.
todo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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