uphold/koa-error-mapper

Name: koa-error-mapper

Owner: Uphold

Description: koa error mapper

Created: 2015-03-10 15:24:32.0

Updated: 2017-03-22 18:44:11.0

Pushed: 2017-03-23 11:04:46.0

Homepage:

Size: 64

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

koa-error-mapper

koa-error-mapper is a middleware that handles application errors by mapping them to a custom format.

Status

npm version build status

Installation

Install the package via yarn:

rn add koa-error-mapper

or via npm:

m install koa-error-mapper --save
Usage
Mappers

Mappers are responsible for handling errors and normalizing the response. A generic fallback mapper is included in case no custom mapper is available.

The only interface for a mapper is a map() function. A mapper should return undefined if it is not capable or responsible for mapping a specific error. It must return an object with status and body. If not returned on the object, the value on the response will be undefined for precaution. It may also contain a headers property.

le.exports = {
p(e) {
if (!(e instanceof CustomError)) {
  return;
}

// Add your own custom logic here.
return { status: e.code, body: { message: e.message }, headers: { Foo: 'Bar' }};


Now use the error mapper and register CustomMapper:

 strict';

t CustomError = require('path/to/my/custom/error');
t Koa = require('koa');
t app = new Koa();
t customMapper = require('path/to/my/custom/mapper');

use(errorMapper([customMapper]);

get('/', async () => {
row new CustomError(401, 'Ah-ah!');


listen(3000);

Result:

/

/1.1 401 Unauthorized
 Bar

essage": "Ah-ah!" }
Tests
rn test
Release
m version [<new version> | major | minor | patch] -m "Release %s"
License

MIT


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.