I have an angular 2 service which calls an http endpoint in its constructor to retrieve current user. The endpoint throws a 401 if the user has not been authenticated.
My issue is that NG2 logs the exception even though I eat the error.
Here is my service function:
getVal(): Observable<TestData | boolean> {
return this.http.get('badUrl', new JsonGetOptions())
.map(this.jsonParser)
.catch((error: any) => {
if (error.status && (error.status === 401 || error.status === 404)) {
return Observable.of(false);
}
return Observable.throw(error);
});
}
And here is the service constructor:
constructor(public http: Http) {
this.valSource = new ReplaySubject<TestData | boolean>(1);
this.val$ = this.valSource.asObservable();
this.getVal()
.subscribe(val => {
this.valSource.next(val);
});
}
Here is the Plunker
I have simplified the code and changed it to eat HTTP/404 errors to work in plunker.
Everything works as expected. It's just annoying that I see the console error.