-
Notifications
You must be signed in to change notification settings - Fork 2
/
exception.js
29 lines (27 loc) · 1.03 KB
/
exception.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
var extend = require('../extend')
var obfuscateUrlPassword = require('../obfuscateUrlPassword')
var prepareForLogging = require('./prepareForLogging')
module.exports = function (logError, logDetailError) {
return function (request, next) {
return next().then(function (response) {
var exceptions = request.options.exceptions
var isException = exceptions === false ? false : typeof exceptions === 'function' ? exceptions(response) : response.statusCode >= 400
if (isException) {
var obfuscatedUrl = obfuscateUrlPassword(request.url)
var msg = request.method.toUpperCase() + ' ' + obfuscatedUrl + ' => ' + response.statusCode + ' ' + response.statusText
if (logError) {
logError(msg)
}
var logResponse = prepareForLogging(response)
if (logDetailError) {
logDetailError(logResponse)
}
var error = extend(new Error(msg), response)
error.url = obfuscatedUrl
throw error
} else {
return response
}
})
}
}