Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catch api 404 response with Angular2 http

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

like image 875
Basit Avatar asked Feb 20 '16 05:02

Basit


1 Answers

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)
    );
like image 116
Thierry Templier Avatar answered Oct 15 '22 13:10

Thierry Templier