Why the responseError is never called?

Hi, I’m using http-fetch-client to request a resource on webapi.
I have put a few interceptors on my httpclient like this:

config.withBaseUrl(app.HttpSettings.BaseUrl)
                .withInterceptor({
                    request(request: Request) {
                        self.logger.info(`Requesting ${request.method} ${request.url}`);
                        return request;
                    },
                    response(response: Response) {
                        self.logger.info(`Response ${response.status} ${response.url}`);
                        if (response instanceof TypeError) {
                            throw Error(response['message']);
                        }
                        if (response.status == 401 && response.url.indexOf(self.httpClient.baseUrl) == 0) {
                            eventAggregator.publish(Events.Unauthorized, null);
                            return Promise.reject(response);
                        }
                        else if (response.status >= 400) {
                            return response.text()
                                .then(resp => {
                                    let json: any = {};
                                    let error = 'Network Error!!';
                                    try {
                                        json = JSON.parse(resp);
                                    }
                                    catch (e) { }

                                    if (json.message)
                                        error = json.message;
                                    else if (json.error)
                                        error = json.error;
                                    else if (response.statusText)
                                        error = response.statusText;
                                    if (error)
                                        throw new Error(error);

                                    return Promise.reject(resp);
                                });
                        }
                        return response;
                    },
                    requestError(error: Error) {
                        if (error !== null)
                            throw Error(error.message);
                        return error;
                    },
                    responseError(error: Error) {
                        if (error !== null)
                            throw Error(error.message);
                        return error;
                    }
                });
        });

When my webapi return a 401 Unauthorized, my responseError is not called.
Why?

Thanks

you need to use config.rejectErrorResponses() to causes responses with unsuccessful status codes to reject.

A fetch() promise will reject with a TypeError when a network error is encountered, although this usually means permission issues or similar — a 404 does not constitute a network error, for example. MDN

3 Likes

Ah, so this is sow it’s done. Thanks

Any reason why you’re not using config.rejectErrorResponses()?

Because I didn’t know about it until Rabah wrote it here. :blush: