Reputation: 431
How I can ignore HTTP response errors and return the response as is from API call in angular 5 I'm using angular HTTP client and this is my code for calling APIs
Api Handler:
get<T>(url:string):Observable<T> {
return this.http.get<T>(url, this.getRequestHeaders()).pipe<T>(
catchError(error => {
return this.handleError(error, () => this.get(url));
}));
}
Handle Error:
protected handleError(error, continuation: () => Observable<any>) {
if (error.status == 401) {
if (this.isRefreshingLogin) {
return this.pauseTask(continuation);
}
this.isRefreshingLogin = true;
return this.authService.refreshLogin().pipe(
mergeMap(data => {
this.isRefreshingLogin = false;
this.resumeTasks(true);
return continuation();
}),
catchError(refreshLoginError => {
this.isRefreshingLogin = false;
this.resumeTasks(false);
if (refreshLoginError.status == 401 || (refreshLoginError.url && refreshLoginError.url.toLowerCase().includes(this.loginUrl.toLowerCase()))) {
this.authService.reLogin();
return throwError('session expired');
}
else {
return throwError(refreshLoginError || 'server error');
}
}));
}
Upvotes: 5
Views: 6233
Reputation: 1679
If you want to continue your normal flow on an error code of 404 and 400 you just need to return a false Observable when you encounter this codes, like this:
import { of } from 'rxjs/observable/of';
...
protected handleError(error, continuation: () => Observable<any>) {
if (error.status == 404 || error.status == 400) {
return of(false);
}
...
}
Upvotes: 3