0

I am not sure how to handle http errors in an angular2 observable. What I have is

    getContextAddress() : Observable<string[]> {
        return this.http.get(this.patientContextProviderURL)
        .map((res:Response) => {
            return res.json()
        })
        .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}

in valid URL cases, i.e. response of 200, all is well. I am not sure why when I use a bad url, response code of 400, there is no error generate.

How do I handle that case?

1
  • What do you mean "there is no error generate"? Where do you subscribe to this? Do you provide the second error handling callback to the subscription? Have any logging in place to see what's happening? Please give a minimal reproducible example. Commented Apr 14, 2017 at 21:33

2 Answers 2

1

This code is simply re-throwing the error. Are you catching it somewhere?

For example, I have code like this:

this.productService.getProducts()
    .subscribe(products => this.products = products,
               error => this.errorMessage = <any>error);
}

This catches the thrown error and sets an errorMessage property that I then display in the UI.

Sign up to request clarification or add additional context in comments.

Comments

0

I was not catching the error where the subscription was taking place. My mistake

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.