In my HttpServive, I have shared http methods that will handle starting/ending the loading bar, setting the proper headers for JWT, and so on.
All is fine, but now I am trying to add some logic in the catch to check the response in the event of a non-200 status code (such as 401), so that I can do something and then re-make the failed request.
The problem is, the response I get back in the event of a non-200 status code is not accessible from that catch for whatever reason.
For instance, this is the code:
// ...
return this.http[method](url, options || defaultOptions)
.map((res: Response) => res.json()) // <--- gives JSON from response
.catch((error: any)) => {
console.log(error);
console.log(error.json()); // <--- should give JSON from response, but gives ProgressEvent instead
return Observable.throw(error.json()); // also gives ProgressEvent when I 'subscribe' to the observable
})
.finally(() => {
this.loadingBarService.complete();
});
The console.log(error) outputs the error of type Response; The console.log(error.json()) outputs some data of type ProgressEvent.
Why am I getting a ProgressEvent, instead of the JSON sent from the non-200 status code response?
This is the ProgressEvent:
{
bubbles: false,
cancelBubbles: false,
cancelable: false,
composed: false,
currentTarget: XMLHttpRequest,
defaultPrevented: false,
eventPhase: 0,
isTrusted: true,
lengthComputable: false,
loaded: 0,
path: Array(0),
returnValue: true,
srcElement: XMLHttpRequest,
target: XMLHttpRequest,
timeStamp: 1234.115,
total: 0,
type: "error",
__proto__: ProgressEvent
}
As you can see, the ProgressEvent is not my response. My response is a JSON object with just one error property, which contains a message and a status. This is just wrong, why don't I get my response?
HttpServiceis custom made by me, and it leverages theHttpservice provided by@angular/http. Hencethis.httpin the example.