hapi.js best way to handle errors

Catfish picture Catfish · Jul 10, 2014 · Viewed 19.2k times · Source

I'm creating my first node.js REST web service using hapi.js. I'm curious as to the best way to handle errors let's say from my dao layer. Do i throw them in my dao layer and then just try/catch blocks to handle them and send back errors in my controller, or is there a better way that the cool kids are handling this?

routes/task.js

var taskController = require('../controllers/task');
//var taskValidate = require('../validate/task');

module.exports = function() {
  return [
    {
      method: 'POST',
      path: '/tasks/{id}',
      config : {
        handler: taskController.createTask//,
        //validate : taskValidate.blah
      }
    }
  ]
}();

controllers/task.js

var taskDao = require('../dao/task');

module.exports = function() {

  return {

    /**
     * Creates a task
     *
     * @param req
     * @param reply
     */
    createTask: function createTask(req, reply) {

      taskDao.createTask(req.payload, function (err, data) {

        // TODO: Properly handle errors in hapi
        if (err) {
          console.log(err);
        }

        reply(data);
      });

    }
}();

dao/task.js

module.exports = function() {

  return {
    createTask: function createTask(payload, callback) {

    ... Something here which creates the err variable...

    if (err) {
      console.log(err); // How to properly handle this bad boy
    }
  }
}();

Answer

nelsonic picture nelsonic · May 15, 2016

Generic Solution w/ Fully Customisable Error Template/Messages

We wrote a Hapi Plugin that handles all errors seamlessly: npmjs.com/package/hapi-error

Build Status codecov.io Code Climate Dependency Status

It lets you define your own custom error pages in 3 easy steps.

1. Install the plugin from npm:

npm install hapi-error --save

2. Include the plugin in your Hapi project

Include the plugin when you register your server:

server.register([require('hapi-error'), require('vision')], function (err) {
 // your server code here ...
});

See: /example/server_example.js for simple example

3. Ensure that you have a View called error_template

Note: hapi-error plugin expects you are using Vision (the standard view rendering library for Hapi apps) which allows you to use Handlebars, Jade, React, etc. for your templates.

Your error_template.html (or error_template.ext error_template.jsx) should make use of the 3 variables it will be passed:

  • errorTitle - the error tile generated by Hapi
  • statusCode - *HTTP statusCode sent to the client e.g: 404 (not found)
  • errorMessage - the human-friendly error message

for an example see: /example/error_template.html

That's it! Now your Hapi App handles all types of errors and you can throw your own custom ones too!

hapi-error-screens

Note: hapi-error works for REST/APIs too. if the content type header (headers.acceps) is set to application/json then your app will return a JSON error to the client, otherwise an HTML page will be served.