forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabort.js
33 lines (28 loc) · 1.03 KB
/
abort.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
import statsd from '../lib/statsd.js'
export default function abort(req, res, next) {
// If the client aborts the connection, send an error
req.once('aborted', () => {
// ignore aborts from next, usually has to do with webpack-hmr
if (req.path.startsWith('/_next')) {
return
}
// NOTE: Node.js will also automatically set `req.aborted = true`
const incrementTags = []
// Be careful with depending on attributes set on the `req` because
// under certain conditions the contextualizers might not yet have
// had a chance to run.
if (req.pagePath) {
incrementTags.push(`path:${req.pagePath}`)
}
if (req.context?.currentCategory) {
incrementTags.push(`product:${req.context.currentCategory}`)
}
statsd.increment('middleware.abort', 1, incrementTags)
const abortError = new Error('Client closed request')
abortError.statusCode = 499
abortError.code = 'ECONNRESET'
// Pass the error to the Express error handler
return next(abortError)
})
return next()
}