<commit_before>import axios, { CancelToken } from 'axios';
import { logoutAction } from 'controllers/auth';

export const ERROR_CANCELED = 'REQUEST_CANCELED';
export const ERROR_UNAUTHORIZED = 'UNAUTHORIZED';

const handleError = (error) => {
  if (axios.isCancel(error)) {
    throw new Error(ERROR_CANCELED);
  }
  if (error.response && error.response.status === 401) {
    throw new Error(ERROR_UNAUTHORIZED);
  }
  if (error.response && error.response.data) {
    throw error.response.data;
  }
  throw error;
};

const handleResponse = (res) => res.data;

export const fetch = (url, params = {}) => {
  const cancelToken = params && params.abort ? new CancelToken(params.abort) : null;
  const headersFromParams = params && params.headers;
  const headers = Object.assign({}, headersFromParams || {});
  const requestParams = {
    ...params,
    cancelToken,
    url,
    headers,
  };
  return axios(requestParams)
    .catch(handleError)
    .then(handleResponse);
};

export const updateToken = (newToken) => {
  axios.defaults.headers.common.Authorization = newToken;
};

export const initAuthInterceptor = (store) => {
  axios.interceptors.response.use(
    (response) => response,
    (error) => {
      if (error.response && error.response.status === 401) {
        store.dispatch(logoutAction());
      }
      return Promise.reject(error);
    },
  );
};
<commit_msg>Fix unnecessary logout actions for unauthorized user<commit_after>import axios, { CancelToken } from 'axios';
import { isAuthorizedSelector, logoutAction } from 'controllers/auth';

export const ERROR_CANCELED = 'REQUEST_CANCELED';
export const ERROR_UNAUTHORIZED = 'UNAUTHORIZED';

const handleError = (error) => {
  if (axios.isCancel(error)) {
    throw new Error(ERROR_CANCELED);
  }
  if (error.response && error.response.status === 401) {
    throw new Error(ERROR_UNAUTHORIZED);
  }
  if (error.response && error.response.data) {
    throw error.response.data;
  }
  throw error;
};

const handleResponse = (res) => res.data;

export const fetch = (url, params = {}) => {
  const cancelToken = params && params.abort ? new CancelToken(params.abort) : null;
  const headersFromParams = params && params.headers;
  const headers = Object.assign({}, headersFromParams || {});
  const requestParams = {
    ...params,
    cancelToken,
    url,
    headers,
  };
  return axios(requestParams)
    .catch(handleError)
    .then(handleResponse);
};

export const updateToken = (newToken) => {
  axios.defaults.headers.common.Authorization = newToken;
};

export const initAuthInterceptor = (store) => {
  axios.interceptors.response.use(
    (response) => response,
    (error) => {
      if (error.response && error.response.status === 401) {
        const isAuthorized = isAuthorizedSelector(store.getState());
        isAuthorized && store.dispatch(logoutAction());
      }
      return Promise.reject(error);
    },
  );
};

{'fraction_non_alphanumeric': 0.13883161512027492, 'fraction_numerical': 0.004123711340206186, 'mean_word_length': 3.7487765089722673, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4506097', 'n_tokens_mistral': 917, 'n_tokens_neox': 921, 'n_words': 199}