I can't seem to get reliable error information out of the HTTP Observable and response object. In some cases it reports the right data, but at other times I get response objects that are effectively empty.
the service that's making the actual call creates the observable with:
return this.http.post(this.config.urls.url("login"), {
username: username,
password: password
}, new RequestOptions({withCredentials:true}));
In the code above the server is returning a 401 with a JSON error response, yet the subscribe() response is neither getting the response data, nor the status code or message. The error handler fires, but the response is useless to determine error info.
I've tried intercepting the error handler on .subscribe() (as above in the component), the .catch() method and .map() (in the service) but in all cases the response has no usable data inside of it. It's impossible to return an error message to the user without a valid response
I have seen this work however, but I can't for the life of me see what's different.
What I'd like to do this:
- Service method makes HTTP call in service
- Service method has .catch( response ) handler and fixes up error object
- Client issues subscribe and has error handler that accepts error object
This worked fine with Promises (.toPromise), but with the raw Observables I can't seem to get proper error info in many situations.
Any suggestions on how to make this work reliably and why is the response not returning status info?
Update:
It turns out the problem was caused by the server side doing automatic auth redirects. The server has been returning 302 requests to login page if a 401 occurred and it looks like the Observables are eating the 302 - not following, nor capturing the status code/original request data.
I'm going to rack this one up to operator error (me) but leave this here in case somebody else runs into this sort of problem. Moral of the story - beware of 302 redirect responses with Observables.