0

I don't know if I am doing something incorrect here or what, but I have a question regarding error handling in angular.

I've gone through several similar questions on the website but they don't answer my question or solve my problem.

So this is basically what I'm trying to do:

var deferObject = $q.defer();
$http(some_url + user_entered_value).then(function(response){
    deferObject.resolve(response.data);
}, function(error){
    //error handling code here
    deferObject.reject(error.data);
});

The server returns an error object which looks like this: {"status":"500","error":"Internal Server Error"}

Whenever there is an error in the application, angular throws an error by logging it in the console: "failed to load resource: Internal server error".

So here are my questions:

  1. How do I prevent the angular error logging?

  2. How do I handle that error?

I tried doing this:

var deferObject = $q.defer();
$http(some_url + user_entered_value).then(function(response){
    deferObject.resolve(response.data);
}, function(error){
    //error handling code here
    deferObject.reject(error.data);
}).catch(function(e){
    throw e;//to handle the error
});

But I still get the error in the console.

  • 2
    Are you sure angular is throwing the error and not your browser? It's normal for a browser to report network errors in the console. – Matthew Green Dec 21 '16 at 20:25
  • @MatthewGreen In the console, it says that the source of the log is angular.min.js:103 –  Dec 21 '16 at 20:45
  • How do you handle the rejected promise in the caller function? – Anthony C Dec 21 '16 at 20:48
  • @AnthonyC I'm using the exact same code provided above. –  Dec 21 '16 at 20:50
  • Updating your question with the actual error message in the console might help then, @Nachiket. – Matthew Green Dec 21 '16 at 20:54
  • Can you post the code where you handle the rejection? All I see is you are rejecting the promise `deferObject.reject(error.data);` but I don't see where you handle the rejection. Unhandled rejection will result an uncaught exception. That's probably why you are seeing the error in the console. – Anthony C Dec 21 '16 at 20:56
  • @AnthonyC In the console it says that "Failed to load resource: the server responded with a status of 500 (Internal Server Error)" –  Dec 21 '16 at 20:56
  • @AnthonyC Well, I think that the source of the error is the XMLHttpRequest object. I was going through a question [link](http://stackoverflow.com/questions/22113286/prevent-http-errors-from-being-logged-in-browser-console). I would still like to know how to handle errors in general. Say if I get an error how do I handle it in the error function –  Dec 21 '16 at 21:01
  • @AnthonyC I want to know how you handle the error –  Dec 21 '16 at 21:03

0 Answers0