-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
93 lines (80 loc) · 2.12 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Module dependencies.
*/
var swig = require('swig');
var http = require('http');
var env = process.env.NODE_ENV || 'development';
/**
* Expose `error`.
*/
module.exports = error;
/**
* Error middleware.
*
* - `template` defaults to ./error.html
* - `all` handle all type error
* - `text` text error handler
* - `json` json error handler
* - `html` html error handler
* - `debug` whether to print error.stack in console
* - `alias` error.status alias
*
* @param {Object} opts
* @api public
*/
function error(opts) {
opts = opts || {};
// template
var path = opts.template || __dirname + '/error.html';
var render = swig.compileFile(path);
/**
* defaultSettings for hander error
*/
var defaultSettings = {
text: function (err) {
if ('development' == env) return err.message;
else if (err.expose) return err.message;
else throw err;
},
json: function (err) {
if ('development' == env) return { error: err.message };
else if (err.expose) return { error: err.message };
else return { error: http.STATUS_CODES[this.status] };
},
html: function (err) {
return render({
env: env,
ctx: this,
request: this.request,
response: this.response,
error: err.message,
stack: err.stack,
status: this.status,
code: err.code
});
}
};
return function *error(next) {
try {
yield* next;
if (404 == this.response.status && !this.response.body) this.throw(404);
} catch (err) {
if (opts.debug) console.error(err.stack);
if (opts.alias && ('object' === typeof opts.alias)) {
this.status = opts.alias[err.status] || 500;
} else {
this.status = err.status || 500;
}
// application
this.app.emit('error', err, this);
if (opts.all) {
this.body = opts.all.call(this, err) || this.body;
return;
}
var acceptType = this.accepts('html', 'text', 'json');
if (acceptType) {
this.body = (opts[acceptType] || defaultSettings[acceptType]).call(this, err) || this.body;
}
}
}
}