A
A
Al2019-06-05 16:10:55
Angular
Al, 2019-06-05 16:10:55

Why doesn't the interceptor return the ErrorResponse to the handler?

I'm trying to write an interceptor that will prevent the script from exiting in case of an error from the server.
This is how I call the service method in the component:

const res = await this.service.getLandingPage(url);
// do actions with res
console.log(res)

The service method itself:
async getLandingPage<T>(url: string): Promise<Page> {
    return this.get<Page>(url,
      {
        observe: "response"
      }
    ).toPromise();
  }

Interceptor:
return next.handle(request).pipe(
      catchError((error: HttpErrorResponse) => {
        this.log.error(error);
        return EMPTY;
      })
    );

In this case, if the response from the server is successful , HttpResponse arrives in res as expected, and if the server responds with an error, then nothing ( undefined ) in res. If error is returned instead of an empty Observable in the interceptor, then the script will fall and the script will not reach the console.log(res) line in the component . How to make the interceptor so that in the above case, when an error occurs from the server, HttpErrorResponse gets into res and not undefined ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-06-05
@Xuxicheta

return throwError(error)
catchError requires an observable to be returned, and you give it a plain value

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question