How to ignore http response errors when call APIs angular
How to ignore http response errors when call APIs angular
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 => (refreshLoginError.url && refreshLoginError.url.toLowerCase().includes(this.loginUrl.toLowerCase())))
this.authService.reLogin();
return throwError('session expired');
else
));
@DZDomi I updated my question, this is my code for catch error but I want if the status code is 404 or 400 return response that came from API as is
– Musab.BA
Sep 16 '18 at 10:16
1 Answer
1
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>) error.status == 400)
return of(false);
...
Thanks for the answer but when adding your code it gives me an error on (.of) Property 'of' does not exist on type 'typeof Observable'
– Musab.BA
Sep 16 '18 at 10:29
see updated answer
– DZDomi
Sep 16 '18 at 10:32
Thanks for the answer now (of) work but when checking data returned from API I found it equal (false)
– Musab.BA
Sep 16 '18 at 10:46
Thanks for the answer everything works perfectly now when return (of(error.error))
– Musab.BA
Sep 16 '18 at 13:10
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
what have you tried so far?
– DZDomi
Sep 16 '18 at 9:47