I have a global error handler function for $http request.
var errorHandler = function (err) {
// log the error
return {status: err.status, data: {result: -1}}
}
var get = function (url) {
var i = 1 // I have a temp variable here, and want to pass it to errorHandler
return $http({
method: 'GET',
url: ***,
timeout: 5000,
}).then(function (response) {
// do some thing
return response
}, errorHandler)
}
How can I pass the variable i to errorHandler? I know I can do this:
var get = function (url) {
var i = 1 // I have a temp variable here, and want to pass it to errorHandler
return $http({
method: 'GET',
url: ***,
timeout: 5000,
}).then(function (response) {
// do some thing
return response
}, function (err) {
// log the error
console.log('i', i) // access i
return {status: err.status, data: {result: -1}}
})
}
But if I want use a global errorHandler, what should I do?
==============
Update:
According to Leandro Zubrezki's answer, we can make it in this way:
var errorHandler = function (err, i) {
// log the error
return {status: err.status, data: {result: -1}}
}
var get = function (url) {
var i = 1 // I have a temp variable here, and want to pass it to errorHandler
return $http({
method: 'GET',
url: ***,
timeout: 5000,
}).then(function (response) {
// do some thing
return response
}, function(error){
errorHandler(error, i)
})
}
This is a stupid question from newbie. :)
errorHandler (err,{i:i});catch add a value parameter in that method