-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror-handling.js
33 lines (27 loc) · 946 Bytes
/
error-handling.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
'use strict';
const log = require('./logger');
const api = require('./api');
const respondWithError = function(res, requestId, message) {
res
.status(500)
.set({
'cache-control': 'no-cache',
'content-type': 'text/html',
'X-RequestID': requestId
})
.send(`<html><head><title>Error</title></head><body><pre>${message}</pre><pre>RequestID: ${requestId}</pre></body></html>`);
};
const errorHandlingMiddleware = function (err, req, res, next) {
log.error(err);
log.flush();
if (process.env.NODE_ENV == 'production') {
respondWithError(res, log.requestId(), err.message);
} else {
if (err instanceof api.NonCanonicalParamsError) {
res.redirect(req.path + '?' + err.canonicalQs);
return;
}
respondWithError(res, log.requestId(), err.stack);
}
};
module.exports.middleware = errorHandlingMiddleware;