/hapi-boom-decorators

Decorates a Hapi server's reply interface with functions to make it easy to reply with Boom errors

Primary LanguageJavaScriptMIT LicenseMIT

hapi-boom-decorators

Circle CI

A plugin for hapi.js to make responding with boom errors a little less verbose by decorating the reply interface with equivilent methods.

Install

npm install hapi-boom-decorators --save

Register Plugin

server.register({
  register: require('hapi-boom-decorators')
}, function(err) {
  ...
});

API

Standard way of replying with boom response:

server.route({
  method: 'GET',
  path: '/resource/{id}',
  handler: (request, reply) => {
    reply(Boom.notFound());
  }
});

New way:

server.route({
  method: 'GET',
  path: '/resource/{id}',
  handler: (request, reply) => {
    reply.notFound();
  }
});

Check the boom documentation for all available methods. Every 4xx and 5xx error type has been implemented in hapi-boom-decorators with the same function signature e.g. reply.xxx([message], [data]).

  • wrap - reply(Boom.wrap(err, 500, 'a message')) can be written as reply.boom(500, err, 'a message')
  • create - reply(Boom.create(500, 'a message', {})) can be written as reply.boom(500, 'a message', {})