softonic / axios-retry

Axios plugin that intercepts failed requests and retries them whenever possible
Other
1.9k stars 167 forks source link

Axios-retry swallows the error response #274

Closed lm-dlukmanov closed 5 months ago

lm-dlukmanov commented 5 months ago

Using axios-retry in NestJs app like this:

retryWrapper(this.client, {
            retryDelay: (retryCount) => {
                this.logger.log(`axios retry attempt № ${retryCount}`);
                return retryCount * +config.axios.retriesMultiplierMs;
            },
            retryCondition: (error) => {
                const errMessage = { errorData: error.response?.data || error };
                if (error.response?.config?.["axios-retry"]) {
                errMessage['axiosRetry'] = error.response.config["axios-retry"];
                }
                this.logger.error(`axios retry error: ${JSON.stringify(errMessage)}`);
                return !!error;
            },
            retries: +config.axios.retriesCounter,
        });

After the last retry as expected I should get an error:

try {
    someLogic();
} catch(err) {
    console.log(err) // And here is the error thrown after the last retry
}

The point is - the error thrown after last retry does not include all the information of the root error. I only get this structure:

{
    message: "Request failed with status code 400", // as an example
    name: "AxiosError",
    config: "some stuff",
    code: "ERR_BAD_REQUEST",
    status: 400
}

whereas the root error have a lot of additional data in error response, i.e.:

{status: 400, {"errors":[{"code":"118","message":"MY_SERVICE service required for tariff MY_TARIFF"}]}}

How can I get the whole error object not only some of the fields which are not very informative?

yutak23 commented 5 months ago

would you try console.log(err.response.data)?

lm-dlukmanov commented 5 months ago

@yutak23 Yes. And I see there additional data. But when I catch error it doesn't have such field response

lm-dlukmanov commented 5 months ago

I decided to use interceptor of axios and handle it on my side.