8

I have api which returns 400 or 404 errors if failed.

When I run the http.get method, it does not catch the error on subscribe and throws error.

Http.get(`api/path/here`).map(res => res.json()).subscribe(
            data => console.log(data),
            error => console.log(error)
        );

Following is the error I get

enter image description here

2
  • take a look in the network tab for status code and in the preview tab for error information Commented Feb 20, 2016 at 5:28
  • @PardeepJain status code is 400 that whats the api returns, if user object is not found. Commented Feb 20, 2016 at 5:31

2 Answers 2

11

You could also use the catch operator

http.get(`api/path/here`)
    .map(res => res.json())
    .catch(
        err => {
          // handle errors
        })
    .subscribe(
        data => console.log(data)
    );
Sign up to request clarification or add additional context in comments.

1 Comment

Does the "catch" have any benefit over utilizing the "onError" method on the subscribe operator itself? If I simply include an "onError" method implementation, is there a chance I could have uncaught exceptions possibly caused by my "map" operator? Thanks!
9
.subscribe(res=>{
    this.data=res;
    console.log('bye');
 },
 (err)=>console.log(err),
 ()=>console.log("Done")
 );

Try out this way.

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.