From the course: Angular: API Communication and Authentication

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Central error catching

Central error catching - Angular Tutorial

From the course: Angular: API Communication and Authentication

Start my 1-month free trial

Central error catching

- [Instructor] Our API service would not be complete with some kind of error catching. another benefit of having this API service is that we also have a central place where we can deal with HTTP errors. In the request method, we can catch any HTTP errors by using the catch operator. We will need to import this operator. Then, if any errors occur, we can delegate to a new onRequestError function. They only pass along the response that we get back. This new onRequestError method on the API service takes in the response and in this method, we can extract the service code in the body of the function. Since we have followed good API standards of returning both a status code and an error message for any API errors, this will make it easier to handle in our UI. From here, we can create an error object which will hold the status code and the error message sent from the API. We can log this all to the console so can see any errors and lastly, we want to return an observable that emits any…

Contents