-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patherror.js
60 lines (49 loc) · 1.22 KB
/
error.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
'use strict';
/**
* Module dependencies.
*/
var http = require('http');
/**
* Expose `error`.
*/
module.exports = error;
function error() {
// env
var env = process.env.NODE_ENV || 'development';
return function *error(next) {
try {
yield next;
if (404 == this.response.status && !this.response.body) {
this.throw(404);
}
} catch (err) {
this.status = err.status || 500;
// application
this.app.emit('error', err, this);
// accepted types
switch (this.accepts('html', 'text', 'json')) {
case 'text':
if ('development' == env) {
this.body = err.message;
} else if (err.expose) {
this.body = err.message;
} else {
throw err;
}
break;
case 'json':
if ('development' == env) {
this.body = { message: err.message };
} else if (err.expose) {
this.body = { message: err.message };
} else {
this.body = { message: http.STATUS_CODES[err.status] };
}
break;
case 'html':
yield this.render('error', { message: err.message });
break;
}
}
}
}