5

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');
          }
        }));
    }
2
  • 1
    what have you tried so far? Commented Sep 16, 2018 at 9:47
  • 1
    @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 Commented Sep 16, 2018 at 10:16

1 Answer 1

3

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);
    }
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

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'
Thanks for the answer now (of) work but when checking data returned from API I found it equal (false)
Thanks for the answer everything works perfectly now when return (of(error.error))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.