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
Size: 64
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
koa-error-mapper
is a middleware that handles application errors by mapping them to a custom format.
Install the package via yarn
:
rn add koa-error-mapper
or via npm
:
m install koa-error-mapper --save
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!" }
rn test
m version [<new version> | major | minor | patch] -m "Release %s"
MIT