catch api 404 response with Angular2 http
10,153
Solution 1
.subscribe(res=>{
this.data=res;
console.log('bye');
},
(err)=>console.log(err),
()=>console.log("Done")
);
Try out this way.
Solution 2
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)
);
Author by
Basit
Updated on July 29, 2022Comments
-
Basit over 1 year
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
-
cjones26 over 6 yearsDoes 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!