error-handler.js
2.9 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
// # boot - error handler
var _ = require('underscore')
var util = require('util')
exports = module.exports = function(logger, settings) {
return function() {
var app = this
app.use(function(err, req, res, next) {
// set default error status code
res.statusCode = (_.isNumber(err.status)) ? err.status : 500
if (!_.isString(err.message))
err.message = 'An unknown error has occured, please try again'
if (_.isObject(err) && _.isNumber(err.code) && err.code === 11000) {
// <https://github.com/LearnBoost/mongoose/issues/2129>
var field = err.message.split('index: test.')[1].split('.$')[1]
// now we have `email_1 dup key`
field = field.split(' dup key')[0]
field = field.substring(0, field.lastIndexOf('_'))
err.message = util.format('Duplicate %s already exists in database, try making a more unique value', field)
err.param = field
}
// if we pass an error object, then we want to simply return the message...
// if we pass an object, then we want to do a stack trace, and then return the object + stack
var error = {}
// set error type
error.type = _.isString(err.param) ? 'invalid_request_error' : 'api_error'
if (error.type === 'invalid_request_error' && res.statusCode === 500)
res.statusCode = 400
// set error message and stack trace
if (util.isError(err)) {
error.message = err.message
} else {
_.extend(error, err)
}
// set status code for BadRequestError
if (_.isString(error.name) && error.name === 'BadRequestError') {
error.type = 'invalid_request_error'
res.statusCode = 400
delete error.name
}
if (settings.showStack)
error.stack = _.isUndefined(err.stack) ? new Error(err.message).stack : err.stack
// set error level
var level = (res.statusCode < 500) ? 'warn' : 'error'
logger[level](error)
// set error back to warning if it was warn
// logger level type = "warn"
// req.flash messages type = "warning"
if (level === 'warn')
level = 'warning'
// if we have a mongoose validation err
// then we know to output all the errors
if (_.isObject(error.errors) && !_.isEmpty(error.errors)) {
var messages = []
_.each(error.errors, function(errMsg) {
if (_.isString(errMsg.message))
messages.push(errMsg.message)
})
if (!_.isEmpty(messages))
error.message = messages.join(' ')
}
res.format({
text: function() {
res.send(error.message)
},
html: function() {
req.flash(level, error.message)
res.redirect('back')
},
json: function() {
res.json({ error: error })
}
})
})
}
}
exports['@singleton'] = true
exports['@require'] = [ 'igloo/logger', 'igloo/settings' ]