-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (58 loc) · 1.4 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
/**
* Dependencies
*/
var render = require('co-render');
var http = require('http');
/**
* Expose `error`
*/
module.exports = errors;
/**
* Error middleware
*
* - `template` defaults to ./errors.html
*
* @param {Object} opts
*/
function errors(opts) {
opts = opts || {};
// Template
var template = opts.template || __dirname + '/errors.html';
var engine = opts.engine || 'swig';
// Environment
var env = process.env.NODE_ENV || 'development';
return function *errors(next){
try {
yield next;
if ((this.status == 404)||(!this.response.body))
this.throw(404);
}
catch (error) {
this.status = error.status || 500;
// Error event
this.app.emit('error', error, this);
// Accepted types
switch (this.accepts('html', 'json')) {
case 'json':
if (env === 'development')
this.body = { error: error.message };
else
this.body = { error: http.STATUS_CODES[this.status] + ' (' + this.status + ')' }
break;
default:
this.body = yield render(template, {
env: env,
ctx: this,
request: this.request,
response: this.response,
error: error.message,
stack: error.stack,
status: this.status,
code: error.code,
engine: engine
});
break;
}
}
}
}