I think that the purest way would be to turn the body of the entire function into an observable, since it will turn any random error into an observable error. Something like that:
get(endpoint: string): Observable { return Rx.Observable.defer(() => { var headers = new Headers(); this._appendAuthentificationHeaders(headers, this.user.credentials); return Rx.Observable.just(headers); }) .flatMap(headers => this.http.get(endpoint, { headers: headers })) .map(res => res.json()); }
However, I still disagree that http.get returns an observable instead of a promise. Since these are unambiguous observables, your function can be a simple asynchronous function (sry, js instead of ts):
async get(endpoint) { var headers = new Headers(); this._appendAuthentificationHeaders(headers, this.user.credentials); const res = await this.http.get(endpoint, { headers })).toPromise(); return res.json(); }
source share