apero
apero

Reputation: 1154

How to get status code in http request in Angular2

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?

Upvotes: 2

Views: 3183

Answers (1)

DDRamone
DDRamone

Reputation: 1158

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);

Upvotes: 3

Related Questions