Elad Benda Elad Benda - 3 months ago 27
HTTP Question

What's the observable equivalent to `Promise.reject`

I had this code

return this.http.get(this.pushUrl)
.toPromise()
.then(response => response.json().data as PushResult[])
.catch(this.handleError);


I wanted to use
observable
instead of
Promise


how can i return the error to the calling method?

What's the equivalent to
Promise.reject
?

doSomeGet() {
console.info("sending get request");

this.http.get(this.pushUrl)
.forEach(function (response) { console.info(response.json()); })
.catch(this.handleError);
}

private handleError(error: any) {
console.error('An error occurred', error);
// return Promise.reject(error.message || error);
}
}


the calling method was:

getHeroes() {
this.pushService
.doSomeGet();
// .then(pushResult => this.pushResult = pushResult)
// .catch(error => this.error = error);
}

Answer
private handleError(error: any) {
  return Observable.throw('Some error information);
}

See also How to catch exception correctly from http.request()?

Comments