This repository was archived by the owner on Nov 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (102 loc) · 2.96 KB
/
index.js
File metadata and controls
130 lines (102 loc) · 2.96 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var fs = require('fs'),
path = require('path'),
pjoin = path.join,
basename = path.basename,
defaultLogger,
hasOwn = Object.prototype.hasOwnProperty,
ex = {};
var ERRORS_DIR = pjoin(__dirname, 'errors');
function Errors(app, config) {
config || (config = { });
app.set('view engine') ||
(config.plain = true);
config.list || (config.list = [
'badRequest',
'conflict',
'forbidden',
'notFound',
'unauthorized'
]);
this._logger = config.logger || this._defaultLogger;
this._config = config;
this._app = app;
this._errors = { };
this._loadErrors(config.list);
this._bind();
}
Errors.prototype.get = function(key) {
return key?
this._errors[key] :
this._errors;
};
Errors.prototype.set = function(key, decl) {
this._errors[key] = this._create(decl);
return this;
};
Errors.prototype.unset = function(key) {
delete this._errors[key];
return this;
};
Errors.prototype._create = function(decl) {
var Fn = function() {
Object.keys(decl).forEach(function(key) {
this[key] = decl[key];
}, this);
Error.call(this, this.message);
Error.captureStackTrace(this, Fn);
};
Fn.prototype = Error.prototype;
return Fn;
};
Errors.prototype._loadErrors = function(list) {
list.forEach(function(item) {
var decl;
try {
decl = require(pjoin(ERRORS_DIR, item));
} catch(e) {
if (e.code === 'MODULE_NOT_FOUND') {
decl = require(item);
} else {
throw e;
}
}
this.set(decl.name, decl);
}, this);
};
Errors.prototype._defaultLogger = {
error: function(err) {
console.error(new Date().toLocaleString(), '>>', err);
console.log(err.stack);
}
};
Errors.prototype._bind = function() {
var errors = this.get(),
config = this._config,
logger = this._logger,
app = this._app;
app.use(function(err, req, res, next) {
if(!err.name || err.name == 'Error' || !hasOwn.call(errors, err.name)) {
logger.error(err);
res.status(500);
if(req.xhr || config.plain)
return res.send({ error: 'Internal error' });
return res.render('errors/500', {
layout: config.layout,
error: err,
showStack: app.settings.showStackError,
title: 'Oops! Something went wrong!'
});
}
res.status(err.status);
if(req.xhr) return res.send({ error: err.message });
config.plain?
res.send(err.message) :
res.render((config.errorViews || 'errors') + '/' + err.status, {
layout: config.layout,
error: err,
showStack: app.settings.showStackError,
title: err.message
});
});
};
module.exports = Errors;