forked from barc/express-error
-
Notifications
You must be signed in to change notification settings - Fork 1
/
expressError.js
45 lines (38 loc) · 1.45 KB
/
expressError.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
'use strict';
var parsetrace = require('parsetrace'),
jade = require('jade'),
path = require('path');
module.exports = function (options) {
options = options || {};
options.sources = options.sources || true;
options.depth = options.depth || 5;
return function (err, req, res, next) {
var statusCode = err.status || res.statusCode || 200;
function sendHtml(err, html) {
if (err) { next(err); } else { res.status(statusCode).send(html); }
}
try {
if (err.stack) {
var trace = parsetrace(err, options).object();
jade.renderFile(path.join(__dirname, 'view/error.jade'), {
title: options.title || 'Error',
statusCode: statusCode > 299 ? statusCode : 500,
trace: trace,
pretty: true
}, sendHtml);
} else {
jade.renderFile(path.join(__dirname, 'view/object.jade'), {
title: options.title || 'Error',
statusCode: statusCode > 299 ? statusCode : 500,
object: err,
options: options,
util: require('util'),
pretty: true
}, sendHtml);
}
} catch (e) {
console.error('Cannot render this strange error: ', e.stack);
next(err);
}
};
};