Catch api 404 using Angular2 http

I have an api that returns 400 or 404 errors if failed.

When I run the http.get method, it does not catch the error while subscribing and generates an error.

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

The following is the error:

enter image description here

+8
angular response
source share
2 answers
 .subscribe(res=>{ this.data=res; console.log('bye'); }, (err)=>console.log(err), ()=>console.log("Done") ); 

Try this method.

+6
source

You can also use the catch statement.

 http.get(`api/path/here`) .map(res => res.json()) .catch( err => { // handle errors }) .subscribe( data => console.log(data) ); 
+11
source

All Articles