0

I'm not able to catch the http error in the error function call back when posting an http request.

Here is the client side:

$http.post('/updateBuildings', 
            $.param(
                {'element_id': element_id}
            )).success(function(data) {
                tmp.GetBuildings(data.new_building_id);         
            }).error(function(data) {
                console.log('response: ',data);
            });

    };

The server side is returning an http error deliberately : status code: 401, msg: "Sorry, access denied."

http error written by browser to console

The browser (google chrome) writes to the log by itself (see attachment) but I'm not able to catch the error in angular and present an alert for the user for example.

Any ideas?

Thanks

5
  • stackoverflow.com/questions/22113286/… Commented Oct 7, 2014 at 6:55
  • I dont want to prevent the browser from logging. I want to also handle the error in angular Commented Oct 7, 2014 at 7:03
  • Then a try catch block would be the way to go I guess. Commented Oct 7, 2014 at 7:12
  • Do you have any interceptors setup? If so, could they be turning failures into successes? Commented Oct 7, 2014 at 12:27
  • no, dont have such thing Commented Oct 7, 2014 at 14:34

1 Answer 1

1

I've had problems using success and error: they don't behave quite as I expect with respect to promises chaining and errors. While I'm not sure if that's what's going on here, I would suggest not using success and error, but use the standard promise then / catch functions.

$http.post('/updateBuildings', $.param({
  'element_id': element_id
})).then(function(response) {
  return tmp.GetBuildings(response.data.new_building_id);         
}).catch(function(error) {
  console.log('response: ', error.data);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Tried that but still the error is not caught in the angular code

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.