Integrations

Copied
var connect = require("connect");
var Raven = require("raven");

// Must configure Raven before doing anything else with it
Raven.config("https://examplePublicKey@o0.ingest.sentry.io/0").install();

function mainHandler(req, res) {
  throw new Error("Broke!");
}

function onError(err, req, res, next) {
  // The error id is attached to `res.sentry` to be returned
  // and optionally displayed to the user for support.
  res.statusCode = 500;
  res.end(res.sentry + "\n");
}

connect(
  // The request handler be the first item
  Raven.requestHandler(),

  connect.bodyParser(),
  connect.cookieParser(),
  mainHandler,

  // The error handler must be before any other error middleware
  Raven.errorHandler(),

  // Optional fallthrough error handler
  onError
).listen(3000);

Copied
var app = require("express")();
var Raven = require("raven");

// Must configure Raven before doing anything else with it
Raven.config("https://examplePublicKey@o0.ingest.sentry.io/0").install();

// The request handler must be the first middleware on the app
app.use(Raven.requestHandler());

app.get("/", function mainHandler(req, res) {
  throw new Error("Broke!");
});

// The error handler must be before any other error middleware
app.use(Raven.errorHandler());

// Optional fallthrough error handler
app.use(function onError(err, req, res, next) {
  // The error id is attached to `res.sentry` to be returned
  // and optionally displayed to the user for support.
  res.statusCode = 500;
  res.end(res.sentry + "\n");
});

app.listen(3000);

Copied
var koa = require("koa");
var Raven = require("raven");

var app = koa();
Raven.config("https://examplePublicKey@o0.ingest.sentry.io/0").install();

app.on("error", function (err) {
  Raven.captureException(err, function (err, eventId) {
    console.log("Reported error " + eventId);
  });
});

app.listen(3000);

If you’re using Loopback 2.x LTS, make sure you’ve migrated to strong-error-handler, otherwise no errors will get to raven-node.

Configure raven-node as early as possible:

Copied
// server/server.js

const Raven = require("raven");
Raven.config("https://examplePublicKey@o0.ingest.sentry.io/0").install();

Add Raven.errorHandler as a Loopback middleware:

Copied
// server/middleware.json

"final:after": {
  "raven#errorHandler": {},
  "strong-error-handler": {
    "debug": false,
    "log": false
  }
}

Copied
// config/http.js

var Raven = require("raven");
Raven.config("https://examplePublicKey@o0.ingest.sentry.io/0").install();

module.exports.http = {
  middleware: {
    // Raven's handlers has to be added as a keys to http.middleware config object
    requestHandler: Raven.requestHandler(),
    errorHandler: Raven.errorHandler(),

    // And they have to be added in a correct order to middlewares list
    order: [
      // The request handler must be the very first one
      "requestHandler",
      // ...more middlewares
      "router",
      // The error handler must be after router, but before any other error middleware
      "errorHandler",
      /// ...remaining middlewares
    ],
  },
};
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").