Airbrake middleware for Koa

Downloads in past


001.0.05 years ago7 years agoMinified + gzip package size for koa-airbrake in KB


koa-airbrake #
Airbrake middleware for Koa

Installation ##

> npm install --save koa-airbrake

Usage ##

The koa-airbrake middleware intercepts unhandled exceptions in the Koa callstack and reports them to Airbrake. Afterwards, it re-throws the exceptions so that they can continue being processed by the remaining middleware.
const Koa = require('koa');
const app = new Koa();

// Setup your Airbrake client...
const AirbrakeClient = require('airbrake-js');
const airbrake = new AirbrakeClient({  
  projectId: "PROJECT_ID",
  projectKey: 'API_KEY'

// Plugin the "koa-airbrake" middleware
const notifyAirbrake = require('koa-airbrake');

// Let's create a test endpoint that just throws an exception.
app.use(async function(ctx) {
  throw new Error("ka-BOOOOOOOOM!");


Gotchas ##

Exceptions that escape Koa's Callstack ###

This middleware only handles exceptions that are part of the Koa callstack. For example, consider this code that is using a traditional callback:
app.use(function(ctx) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      throw new Error("Oops!  This exception is never rejected!");
    }, 1000);

Since the exception is never passed to the reject callback, it escapes Koa's callstack; therefore it will never be observed by the koa-airbrake middleware. Fortunately, the airbrake library itself provides a handler for these exceptions. Just do this:
// This will intercept any exceptions that escape Koa's callstack.