softonic / axios-retry

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

Retry conditions only trigger on AxiosError. #229

Closed shtlrs closed 1 year ago

shtlrs commented 1 year ago

I'm consuming a specific API that returns internal codes in the responses. Example:

HTTP/1.1 200 OK
Content-Type: application/json

{
  "message": "failure message",
  "errorCode": 752
}

The thing is, the HTTP request's status code is 200, which means that AxiosError won't be thrown by the client, and thus, my retry condition won't trigger.

Is this something that:

  1. Has a potential, not so ugly, workaround that can be done on my side ?
  2. Something to be considered for the library, as we can't really guarantee that the consumed API will return erroneous status codes.
nathanpovo commented 1 year ago

You can pass a function of your choice to the retryCondition option that checks for error conditions that are specific to your system.

yutak23 commented 1 year ago

If the status code is 2xx, then I don't think axios-retry is available. The reason is that axios-retry implements an interceptor for non-2xx responses, as you can see in the code

The following is taken from axios' README

axios.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  });

If you want to implement retry processing in 2xx, I think you need to implement the interceptor by yourself referring to the above instead of using axios-retry.

nathanpovo commented 1 year ago

Found a similar issue https://github.com/softonic/axios-retry/issues/100 which describes a way to implement a custom interceptor for 2xx responses.

shtlrs commented 1 year ago

Ok, I see the maintainers don't want to include this behavior since there are workarounds.

I'll be closing this then.

Thanks folks.