error-handler.js
3.8 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
// # boot - error handler
var mergeDefaults = require('merge-defaults')
var _ = require('underscore')
var _str = require('underscore.string')
_.mixin(_str.exports())
var util = require('util')
exports = module.exports = function(logger, settings) {
return 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 (_.isString(err.name) && err.name === 'AuthenticationError')
err.message = 'Invalid login attempt, 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: ')[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 = res.statusCode < 500 ? 'invalid_request_error' : 'api_error'
if (_.isString(err.param)) {
error.type = 'invalid_request_error'
if (res.statusCode === 500)
res.statusCode = 400
}
/*
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(err.name) && err.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'
// if we have a mongoose validation err
// then we know to output all the errors
if (_.isObject(err.errors) && !_.isEmpty(err.errors)) {
var messages = []
_.each(err.errors, function(errMsg) {
// <https://github.com/syntagma/mongoose-error-helper/blob/master/lib/mongoose-error-helper.js>
// TODO: add support for enum, min, and max?
if (_.isString(errMsg.type) && errMsg.type === 'required' && _.isString(errMsg.path)) {
messages.push(util.format('%s is required', _.humanize(errMsg.path)))
} else 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() {
// set error back to warning if it was warn
// logger level type = "warn"
// req.flash messages type = "warning"
req.flash(level === 'warn' ? 'warning' : level, error.message)
res.redirect('back')
},
json: function() {
res.json({ error: error })
}
})
if (_.isObject(req.log)) {
req.log.response_time = new Date().getTime() - req.log.response_time
req.log.status = res.statusCode
req.log.response_type = res.get('Content-Type')
req.log.response_body = error
} else {
req.log = error
}
logger[level](error.message, req.log)
}
}
exports['@singleton'] = true
exports['@require'] = [ 'igloo/logger', 'igloo/settings' ]