diff --git a/index.js b/index.js index 1f14d97..a99b39d 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,10 @@ const Raven = require("raven"); let ravenUrl = process.env.RAVEN_URL; if (ravenUrl) { Raven.config(process.env.RAVEN_URL).install(); + + // The request handler must be the first middleware on the app + app.use(Raven.requestHandler()); + app.use(Raven.errorHandler()); } const html = fs.readFileSync("./index.html", "utf8"); @@ -59,4 +63,22 @@ app.get("/", (req, res) => { } }); +if (ravenUrl) { + // 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.send( + `Sorry! Something went wrong 💥. The maintainers have been notified, but if you'd like, you can raise an issue on github to give more details\n` + ); + res.end(); + }); +} + app.listen(8080, () => console.log("App listening on port 8080"));