arthurfiorette / axios-cache-interceptor

📬 Small and efficient cache interceptor for axios. Etag, Cache-Control, TTL, HTTP headers and more!

Home Page:https://axios-cache-interceptor.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug: axios-cache-interceptor crashed when cancel requests via `axios.CancelToken`

mt-max opened this issue · comments

What happened?

So I make multiple requests via Promise.all to an API and whenever the server returns 401, I will cancelled the remaining requests. However I got the following error from this library.

TypeError: Cannot read properties of undefined (reading 'id')
    at r4 (index.mjs:2:5633)

My axios setup is as following. What happened is the response interceptor checks for 401 and cancel the remaining requests, I believe axios-cache-interceptor tries to access the config in the request but due to cancellation, it became undefined and crashed which subsequently triggers the response interceptors again and ended up hitting the console.error.

There is no issue when I don't cancel the remaining requests.

import axios, { AxiosError, CancelTokenSource } from "axios";
import { setupCache } from "axios-cache-interceptor";

let cancelTokens = [];

const httpClient = setupCache(axios.create({ baseURL: "some-url" }));

httpClient.interceptors.request.use(
  (config) => {
    const cancelToken = axios.CancelToken.source();
    cancelTokens.push(cancelToken);

    config.cancelToken = cancelToken.token;

    return config;
  },
  (error) => Promise.reject(error)
);

const error401Message = "401error";

httpClient.interceptors.response.use(
  (response) => response,
  (error) => {
    const { response, message } = error;

    if (response) {
      const { status } = response;

      if (status === 401) {
        cancelTokens.forEach((cancelToken) => {
          cancelToken.cancel(error401Message);
        });
        cancelTokens = [];
      }
    } else {
      if (message !== error401Message) {
        console.log(error);
      }
    }

    return Promise.reject(error);
  }
);

Thanks for the library and appreciate any solutions or fixes.

axios-cache-interceptor version

v1.2.0

Node / Browser Version

Chrome 116

Axios Version

v1.4.0

What storage is being used

Web Storage

Relevant debugging log output

TypeError: Cannot read properties of undefined (reading 'id')
    at r4 (index.mjs:2:5633)
    at ...[project stack trace] that is calling axios.get


// col 5633 snippet
const s=a.config,i=s.id,c=s.cache,d=a.response;

I can confirm the same issue with axios-cache-interceptor 1.3.0 and axios 1.5.0

Hey @mt-max. Thanks for reproducing this bug!!

Anyone up to a PR?

I got time to debug this today. The problem is that some implementation is throwing an AxiosError without the config object, either internally at axios or either by your implementation. Previously we just checked for errors that were not AxiosError and ignored them.

I just commited a fix for and and will release asap.

Feel free to reopen this issue if needed :)