1

I've coded this http calling method:

public exists(id: string): Observable<boolean> {
    const buildURL = () => map((userId: string) => this.buildIdURL(userId));
    const buildResponse = () => map(() => true);
    const onErrorGetDetails = <T>() => catchError<T, boolean>((error: Response) => this.handleError(error));
    const makeRequest = () => switchMap((url: string) => this.authHttp.head(url));

    return Observable.of(id)
        .pipe(
            buildURL(),
            makeRequest(),
            buildResponse(),
            onErrorGetDetails()
        );
}

So, I'm trying to handle when response is:

  1. an 404, I need to return an Observable.of(false)
  2. or otherwise, return an Observable.throw(error).

Any ideas?

4
  • or otherwise means 2XX and 3XX type of responses? Commented Jun 20, 2019 at 10:23
  • So, what does you code do instead of what you're describing? Where is the code of handleError? Commented Jun 20, 2019 at 10:24
  • handleError is what I'm trying to code. Commented Jun 20, 2019 at 10:49
  • Otherwise, means whichever situation http raises an error. So, onError is emitted in observable. Commented Jun 20, 2019 at 10:50

2 Answers 2

2

I hope I've understood what you need. The error you have from the catchError method should contain the status code. Therefore you can handle it properly.

I didn't give a proper search, but I recall using HttpErrorResponse rather than Response to get more info regarding the HTTP response.

I wrote something like this months ago:

const onErrorGetDetails = <T>() => catchError<T, boolean>((error: HttpErrorResponse) => {
    if (error.status === 404) {
        return of(false)
    }
    return throwError(error)
Sign up to request clarification or add additional context in comments.

Comments

0

To accomplish that you need to set an option called

{observe: 'response'} 

That allow you to read the response status of an http call. So the http call will be:

  makeCall(): Observable<any> {
        return this.http.get('uri', {observe: 'response'});
      } 

And when you will be subscribing just check the error status, you can check the error status even without subscribing, to do that just pipe and use map or catchError and check the Reponse.

Here is an example of how i implemented that on a jwt validation:

return this.auth.validateJwt().pipe(
        map( (response) => {
          if (response.status === 200) {
            return true;
          }
        }),
        catchError((err: Response) => {
          this.handleError('authentication');
          if (err.status === 200) {
            return of(true);
          }

          return of(false);
        }),
        first()
      ); 

I map the response error to see if the code is 200, mapping the response mean that the server will return something, remember we wil receive the full response, so response will be composed by header, body response, where body is the data. But if the server is offline or i type a wrong url on code i catch the error because i still have a response even without a body.

Angular docs: https://angular.io/guide/http#reading-the-full-response

Comments

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.