Name: koa-better-error-handler
Owner: Lad
Description: A better error-handler for Lad and Koa. Makes `ctx.throw` awesome (best used with koa-404-handler)
Created: 2016-07-24 09:42:04.0
Updated: 2018-05-20 15:59:49.0
Pushed: 2018-01-01 02:50:13.0
Homepage: https://lad.js.org
Size: 250
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A better error-handler for Lad and Koa. Makes
ctx.throw
awesome (best used with koa-404-handler)
<ul>
for Mongoose validation errors with more than one messagectx.throw
beautiful messages (e.g. ctx.throw(404)
will output a beautiful error object :hibiscus:)text/html
, application/json
, and text
response typesinstall --save koa-better-error-handler
You should probably be using this in combination with koa-404-handler too!
No support for sessions, cookies, or flash messaging:
t errorHandler = require('koa-better-error-handler');
t Koa = require('koa');
t Router = require('koa-router');
t koa404Handler = require('koa-404-handler');
nitialize our app
t app = new Koa();
verride koa's undocumented error handler
context.onerror = errorHandler;
pecify that this is our api
context.api = true;
se koa-404-handler
use(koa404Handler);
et up some routes
t router = new Router();
hrow an error anywhere you want!
er.get('/404', ctx => ctx.throw(404));
er.get('/500', ctx => ctx.throw(500));
nitialize routes on the app
use(router.routes());
tart the server
listen(3000);
ole.log('listening on port 3000');
Built-in support for sessions, cookies, and flash messaging:
t errorHandler = require('koa-better-error-handler');
t Koa = require('koa');
t redis = require('redis');
t RedisStore = require('koa-redis');
t session = require('koa-generic-session');
t flash = require('koa-connect-flash');
t convert = require('koa-convert');
t Router = require('koa-router');
t koa404Handler = require('koa-404-handler');
nitialize our app
t app = new Koa();
efine keys used for signing cookies
keys = ['foo', 'bar'];
nitialize redis store
t redisClient = redis.createClient();
sClient.on('connect', () => app.emit('log', 'info', 'redis connected'));
sClient.on('error', err => app.emit('error', err));
efine our storage
t redisStore = new RedisStore({
ient: redisClient
dd sessions to our app
use(
nvert(
session({
store: redisStore
})
dd support for flash messages (e.g. `req.flash('error', 'Oops!')`)
use(convert(flash()));
verride koa's undocumented error handler
context.onerror = errorHandler;
se koa-404-handler
use(koa404Handler);
et up some routes
t router = new Router();
hrow an error anywhere you want!
er.get('/404', ctx => ctx.throw(404));
er.get('/500', ctx => ctx.throw(500));
nitialize routes on the app
use(router.routes());
tart the server
listen(3000);
ole.log('listening on port 3000');
Example Request:
-H "Accept: application/json" http://localhost/some-page-does-not-exist
Example Response:
tatusCode": 404,
rror": "Not Found",
essage":"Not Found"
If you specify app.context.api = true
or set ctx.api = true
, and if a Mongoose validation error message occurs that has more than one message (e.g. multiple fields were invalid) ? then err.message
will be joined by a comma instead of by <li>
.
Therefore if you DO want your API error messages to return HTML formatted error lists for Mongoose validation, then set app.context.api = false
, ctx.api = false
, or simply make sure to not set them before using this error handler.
With error lists:
tatusCode": 400,
rror": "Bad Request",
essage": "<ul class=\"text-xs-left mb-0\"><li>Path `company_logo` is required.</li><li>Gig description must be 100-300 characters.</li></ul>"
Without error lists:
tatusCode":400,
rror":"Bad Request",
essage":"Path `company_logo` is required., Gig description must be 100-300 characters."
MIT © Nick Baugh