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

Elad Benda picture Elad Benda · Aug 17, 2016 · Viewed 12.9k times · Source

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

Günter Zöchbauer picture Günter Zöchbauer · Aug 17, 2016
private handleError(error: any) {
    return Observable.throw('Some error information');
}

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