I've got a problem with handling errors in Angular 4. Please advice me the proper approach. When server respond with error I'd like to redirect it to cache block and there I'd like to have a specific behaviour when status is 403. When I check the status in the first map() and throw an error from there - catch block is executed, but error is passing as a string with stack trace. It makes unpossible to recognize status in catch block again. How to do it right way?
getLoggedInUser(): Observable<User> {
return this.http.get(this.baseApiUrl + `api/user/me`)
.map((res: Response) => {
if(res.status < 200 || res.status >= 300) {
console.log(res.status);
throw new Error();
}
else {
return res.json();
}
})
.map((user) => {
let deserializedUser = new User().deserialize(user)
this._loggedInUser = deserializedUser;
return deserializedUser;
})
.catch((errorStatus: any) => {
console.log(errorStatus.status);
if (errorStatus === "403") {
console.log(errorStatus);
this.isRequestForbidden = true;
}
return Observable.throw(errorStatus || 'Server error')
});
}