i'm creating a HttpClient for my Angular2 typescript app and I'm stuck in error handler.
I want to emit an event when I catch a 401 error, and I'm able to catch the error but not to trigger my event, because the .catch needs to be attached to a function of type Observable, so I dont have access of my HttpClient Class this scope.
errorHandler (error:any) : Observable<any> {
if(error.status == 401) {
// I want to call this.myOtherMethod() here!
this.events.publish('token_expired'); // Will throw this.events is undefined
console.log(error);
}
return Observable.throw(error.json());
}
get(url) : Observable<any> {
return this.http.get(url, {
headers: this.customHeaders
}).catch( this.errorHandler );
}
So I know I cant access my this.events from inside this function, but what's the workaround? How can I inject the events there?