Angular httpClient interceptor error handling
You must pass the argument value to the do function of the stream, not create a new function inside it:
return next.handle(request)
.do((err: any) => {
console.log('this log isn't');
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
console.log('nor this one!');
}
}
});
You should catch an error using catchError
return next.handle(request)
.pipe(catchError(err => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
console.log('this should print your error!', err.error);
}
}
}));
Your error handler needs to return a new Observable<HttpEvent<any>>()
return next.handle(request)
.pipe(catchError((err: any) => {
console.log('this log isn't');
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
console.log('Unauthorized');
}
}
return new Observable<HttpEvent<any>>();
}));