In one of my Angular 2 applications, I have a class that contains numerous methods for managing authentication. One particular method is responsible for handling errors thrown by the angular/http module. For example, if a response returns a status code of 401, this handler method will trigger a logout method within the same class using `this.logout()`. However, when I build and test the app in a browser, I encounter an error stating "this.logout() is not a function" whenever an HTTP request responds with a 401 status code.
public handle(error: Response | any, caught: Observable<any>): Observable<ResponseError> {
let errRes: ResponseError;
if (error instanceof Response) {
const body = error.json();
errRes = { message: body.message, statusCode: error.status };
if (body.hasOwnProperty('authCheck')) {
errRes.authCheck = body.authCheck;
}
if (_.inRange(errRes.statusCode, 401, 403)) {
this.logout()
}
} else {
const errMsg = error.message ? error.message : error.toString()
errRes = { message: errMsg }
}
return Observable.throw(errRes)
}