2
votes

This in my interceptor

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req)
      .catch(err => {
        if (err instanceof HttpErrorResponse) {
          this.errorControl(req, err.status);
          return Observable.throw(err);
        }
      });
  }

I need to catch the error sent from the server in my component since I have to act accordingly (404: return to last url, 400: show a message, etc). When I catch the error in the subscription to the observable I get this

"You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable."

but I need a HttpErrorResponse with its status, status text, etc.

Removing the interceptor solves my problem, but I need it for all the session stuff.

1
Could you post the content of your errorControl(req, err.status) function ? - user4676340
Can you show the code that its actually causing the error? How are you catching the error in the subscription - Jota.Toledo

1 Answers

2
votes

Your code is this :

if (err instanceof HttpErrorResponse) {
  this.errorControl(req, err.status);
  return Observable.throw(err);
}

But I don't see an else anywhere !

This means you aren't returning the error when your error isn't an instance of HttpErrorResponse.

You should return an Observable in any case, not only when it suits you.