Skip to content

Commit 9db0003

Browse files
authored
repo sync
2 parents 247659e + 1921855 commit 9db0003

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

middleware/handle-errors.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,24 @@ function shouldLogException (error) {
1717
return true
1818
}
1919

20+
async function logException (error, req) {
21+
if (process.env.NODE_ENV !== 'test' && shouldLogException(error)) {
22+
await FailBot.report(error, {
23+
path: req.path
24+
})
25+
}
26+
}
27+
2028
module.exports = async function handleError (error, req, res, next) {
29+
// If the headers have already been sent...
30+
if (res.headersSent) {
31+
// Report to Failbot
32+
await logException(error, req)
33+
34+
// We MUST delegate to the default Express error handler
35+
return next(error)
36+
}
37+
2138
// if the error is thrown before req.context is created (say, in the Page class),
2239
// set req.context.site here so we can pass data/ui.yml text to the 500 layout
2340
if (!req.context) {
@@ -46,13 +63,10 @@ module.exports = async function handleError (error, req, res, next) {
4663
if (process.env.NODE_ENV !== 'test') {
4764
console.error('500 error!', req.path)
4865
console.error(error)
49-
50-
if (shouldLogException(error)) {
51-
await FailBot.report(error, {
52-
path: req.path
53-
})
54-
}
5566
}
5667

5768
res.status(500).send(await liquid.parseAndRender(layouts['error-500'], req.context))
69+
70+
// Report to Failbot AFTER responding to the user
71+
await logException(error, req)
5872
}

0 commit comments

Comments
 (0)