Installing Airbrake in a hapi.js application

Features

  • Easy and flexible installation options including NPM, Bower and a public CDN
  • Send uncaught errors to Airbrake or manually using a try/catch
  • Add custom parameters to your errors for more context
  • Private sourcemap support
  • Control which errors you send with customizable filtering options

Installation

Using npm

npm install airbrake-js

Using Bower

bower install airbrake-js-client

Configuration

After you have installed the airbrake-js notifier the next step is configuring Airbrake in your app. This involves registering an error handler with a configured AirbrakeClient. Be sure to replace the values for projectId and projectKey with the real values from your project’s settings page. Here is an example hapi.js app that shows the basics of configuration and reports an error to Airbrake when you visit localhost:8000/hello:

'use strict';

const Hapi = require('hapi');
const AirbrakeClient = require('airbrake-js');
const makeErrorHandler = require('airbrake-js/dist/instrumentation/hapi');


// Create Airbrake client
const airbrake = new AirbrakeClient({
  projectId: 1,
  projectKey: 'FIXME'
});

// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
  host: 'localhost',
  port: 8000
});

// Add the route
server.route({
  method: 'GET',
  path: '/hello',
  handler: function hello(request, reply) {
    throw new Error('hello from hapi');
    return reply('hello world');
  }
});

// Register Airbrake error handler
server.register({
  register: makeErrorHandler(airbrake),
}, (err) => {
  if (err) {
    throw err;
  }
});

// Start the server
server.start((err) => {
  if (err) {
    throw err;
  }
  console.log('Server running at:', server.info.uri);
});

Going Further

Installation and configuration is just the beginning. The airbrake-js notifier supports many other advanced uses and options including:

Please visit the airbrake-js GitHub repo for more usage and configuration examples.