Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Fix #1583, use the Raven Express middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ianb committed Nov 17, 2016
1 parent fdac82f commit 9f4a655
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
12 changes: 12 additions & 0 deletions server/src/ravenclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ exports.captureRavenException = function () {
return ravenClient.captureException.apply(ravenClient, arguments);
}
};

exports.addRavenRequestHandler = function (app) {
if (ravenClient) {
app.use(raven.middleware.express.requestHandler(ravenClient));
}
};

exports.addRavenErrorHandler = function (app) {
if (ravenClient) {
app.use(raven.middleware.express.errorHandler(ravenClient));
}
};
32 changes: 17 additions & 15 deletions server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ const { createProxyUrl } = require("./proxy-url");
const statsd = require("./statsd");
const { notFound } = require("./pages/not-found/server");
const { cacheTime, setCache } = require("./caching");
const { captureRavenException, sendRavenMessage } = require("./ravenclient");
const { captureRavenException, sendRavenMessage,
addRavenRequestHandler, addRavenErrorHandler } = require("./ravenclient");
const { errorResponse, simpleResponse, jsResponse } = require("./responses");
const selfPackage = require("./package.json");

Expand Down Expand Up @@ -153,6 +154,8 @@ function addHSTS(req, res) {
}
}

addRavenRequestHandler(app);

app.use((req, res, next) => {
genUuid.generate(genUuid.V_RANDOM, function (err, uuid) {
if (!err) {
Expand Down Expand Up @@ -945,20 +948,6 @@ app.post('/api/fxa-oauth/token', function (req, res, next) {
}).catch(next);
});

app.use(function (err, req, res, next) {
console.error("Error:", err);
console.error(err.stack);
captureRavenException(err);
if (err.isAppError) {
let { statusCode, headers, payload } = err.output;
res.status(statusCode);
res.header(headers);
res.send(payload);
return;
}
errorResponse(res, "General error:", err);
});

if (! config.disableMetrics) {
app.use("/metrics", require("./pages/metrics/server").app);
}
Expand Down Expand Up @@ -1112,6 +1101,19 @@ linker.init().then(() => {

require("./jobs").start();

addRavenErrorHandler(app);

app.use(function (err, req, res, next) {
if (err.isAppError) {
let { statusCode, headers, payload } = err.output;
res.status(statusCode);
res.header(headers);
res.send(payload);
return;
}
errorResponse(res, "General error:", err);
});

/* General 404 handler: */
app.use(function(req, res, next) {
notFound(req, res);
Expand Down

0 comments on commit 9f4a655

Please sign in to comment.