Another option is simply to have a service that delegates the Http service when adding any custom error handling, debug logic, extra headers, etc. that you will need to interact with your API.
This service becomes your centralized point for interacting with everything related to Http, and therefore you can centralize error handling right there. In any project that I am developing, I ALWAYS create such a class, because almost every single API you interact with has some common configuration that should happen with every request (even if this configuration is no less than specifying a base url for use).
An example of such a class:
export class ApiGateway { baseURL = "https://myapi.com"; // or sometimes pulled from another file constructor(private http: Http) { } get(path, params) { showLoadingIndicator(); let headers = this.createMySpecialHeaders(); let options = { headers: headers } // and whatever else you need to generalize let fullUrl = this.baseUrl + path + '?' + this.urlEncode(params)`; return this.get(path, params, options) .do(() => hideLoadingIndicator()) .map(res => res.json()) .catch(err => { hideLoadingIndicator(); // show error message or whatever the app does with API errors etc // sometimes rethrow the error, depending on the use case }) } }
For me, these are the basic principles of OOP - you exchange your interactions with things outside your control in the adapter class to provide you some protection against external changes and change the API of this external thing to something that you prefer, if necessary.
With such a class in place, if, for example, you upgrade to Angular 4 and change the way you get errors, you only have one place to change to handle the new error technique.
source share