2

I'm trying to make a http request and after that return the response object or bool. Doesn't realy matter which one. But i'm can't catch the error. I have a handleError function, but it does not realy work.

My code now looks like this.

The service

updateProduct(product: Product): Promise<number> {
        return this.http.put('/api/products/1' + product.id,product)
            .toPromise()
            .then(response => response.status)
            .catch(this.handleError);
    }

    private handleError(error: any): Promise<any> {
        //console.error('An error occurred', error); // for demo purposes only
        return Promise.reject(error.message || error);
    }

The save function

onSave(): void {
    this.productService.updateProduct(this.product)
        .then(() => this.goBack())
        .catch(er => console.log(er));
}

How can I get this to work?

2
  • so you are getting 4xx status code in response ? Commented Feb 2, 2017 at 13:23
  • What do you mean by "doesn't really work." Keep in mind, you can use the same handler for both success, and failure. You just send it different information based on which response (success or fail) you get. In that handler you can set whatever you want based on that information. So if the handler gets "error", return this, otherwise return that. Commented Feb 2, 2017 at 13:23

1 Answer 1

3

What i see you're returning error.message when it exists

return Promise.reject(error.message || error);

Return whole error object instead of just message if you want to manipulate with that.

return Promise.reject(error);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.