2

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. :)

2
  • all you wanna do is common method for error handling .. if you only need i for this page or business then how come it'll become common ?? if you say i may be pass sometime value then try like this errorHandler (err,{i:i}); catch add a value parameter in that method Commented Nov 26, 2015 at 5:02
  • @AnikIslamAbhi, can you give me a sample, I have tried your method, it can not work. Commented Nov 26, 2015 at 5:42

1 Answer 1

2

Do it like this:

function(error) {
     errorHandler(error, i);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please add an explanation of why this solves the OP's problem. Help others to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.