ladjs/koa-better-error-handler

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

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

koa-better-error-handler

build status code coverage code style styled with prettier made with lass license

A better error-handler for Lad and Koa. Makes ctx.throw awesome (best used with koa-404-handler)

Index
Features
Install
install --save koa-better-error-handler
Usage

You should probably be using this in combination with koa-404-handler too!

API

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');
Web App

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');
User-Friendly Responses

Example Request:

 -H "Accept: application/json" http://localhost/some-page-does-not-exist

Example Response:


tatusCode": 404,
rror": "Not Found",
essage":"Not Found"

HTML Error Lists

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."

License

MIT © Nick Baugh


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.