forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandle-errors.js
80 lines (67 loc) · 2.43 KB
/
handle-errors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const { liquid } = require('../lib/render-content')
const layouts = require('../lib/layouts')
const FailBot = require('../lib/failbot')
const loadSiteData = require('../lib/site-data')
const builtAssets = require('../lib/built-asset-urls')
function shouldLogException (error) {
const IGNORED_ERRORS = [
// avoid sending CSRF token errors (from bad-actor POST requests)
'EBADCSRFTOKEN',
// Client connected aborted
'ECONNRESET'
]
if (IGNORED_ERRORS.includes(error.code)) {
return false
}
// We should log this exception
return true
}
async function logException (error, req) {
if (process.env.NODE_ENV !== 'test' && shouldLogException(error)) {
await FailBot.report(error, {
path: req.path
})
}
}
module.exports = async function handleError (error, req, res, next) {
try {
// If the headers have already been sent or the request was aborted...
if (res.headersSent || req.aborted) {
// Report to Failbot
await logException(error, req)
// We MUST delegate to the default Express error handler
return next(error)
}
// if the error is thrown before req.context is created (say, in the Page class),
// set req.context.site here so we can pass data/ui.yml text to the 500 layout
if (!req.context) {
const site = await loadSiteData()
req.context = { site: site[req.language || 'en'].site, builtAssets }
}
// display error on the page in development, but not in production
if (process.env.NODE_ENV !== 'production' && req.context) {
req.context.error = error
}
// Special handling for when a middleware calls `next(404)`
if (error === 404) {
return res
.status(404)
.send(await liquid.parseAndRender(layouts['error-404'], req.context))
}
// If the error contains a status code, just send that back. This is usually
// from a middleware like `express.json()` or `csrf`.
if (error.statusCode || error.status) {
return res.sendStatus(error.statusCode || error.status)
}
if (process.env.NODE_ENV !== 'test') {
console.error('500 error!', req.path)
console.error(error)
}
res.status(500).send(await liquid.parseAndRender(layouts['error-500'], req.context))
// Report to Failbot AFTER responding to the user
await logException(error, req)
} catch (error) {
console.error('An error occurred in the error handling middleware!', error)
return next(error)
}
}