<commit_before>import axios from 'axios';
import { selectToken, logout } from '../../features/auth/ducks';
import { startApiCall, finishApiCall } from '../../features/spinner/ducks';
import config from '../../config';
import AlertService from './alert';

const normalizeSuccessResponse = response => ({
  ...response,
  ok: true,
});

const normalizeErrorResponse = error => ({
  ...error,
  ok: false,
});

const apiClient = axios.create({
  responseType: 'json',
  baseURL: config.apiUrl,
});

export default apiClient;

export function prepareRequestInterceptor(store) {
  apiClient.interceptors.request.use(config => {
    const token = selectToken(store.getState());

    if (token) {
      config.headers.common = config.headers.common || {};
      config.headers.common['Authorization'] = `Bearer ${token}`;
    }

    store.dispatch(
      startApiCall({
        apiCallId: config.apiCallId,
      })
    );

    return config;
  });
}

export function handleResponsesInterceptor(store) {
  apiClient.interceptors.response.use(
    response => {
      store.dispatch(
        finishApiCall({
          apiCallId: response.config.apiCallId,
        })
      );

      return normalizeSuccessResponse(response);
    },
    error => {
      store.dispatch(
        finishApiCall({
          apiCallId: error.config.apiCallId,
          error,
        })
      );

      if (error.response && error.response.status === 401) {
        store.dispatch(logout());
      }

      showErrorMessage(error);

      return normalizeErrorResponse(error);
    }
  );
}

function showErrorMessage(error) {
  const errorMsg = error.response
    ? error.response.data.message || error.response.data.error.inner
    : error;

  if (Array.isArray(errorMsg)) {
    AlertService.error(errorMsg.reduce((acc, curr) => `${acc}\n${curr}`, ''));
  } else {
    AlertService.error(`${errorMsg}`);
  }
}<commit_msg>fix error handling<commit_after>import axios from 'axios';
import { selectToken, logout } from '../../features/auth/ducks';
import { startApiCall, finishApiCall } from '../../features/spinner/ducks';
import config from '../../config';
import AlertService from './alert';

const normalizeSuccessResponse = response => ({
  ...response,
  ok: true,
});

const normalizeErrorResponse = error => ({
  ...error,
  ok: false,
});

const apiClient = axios.create({
  responseType: 'json',
  baseURL: config.apiUrl,
});

export default apiClient;

export function prepareRequestInterceptor(store) {
  apiClient.interceptors.request.use(config => {
    const token = selectToken(store.getState());

    if (token) {
      config.headers.common = config.headers.common || {};
      config.headers.common['Authorization'] = `Bearer ${token}`;
    }

    store.dispatch(
      startApiCall({
        apiCallId: config.apiCallId,
      })
    );

    return config;
  });
}

export function handleResponsesInterceptor(store) {
  apiClient.interceptors.response.use(
    response => {
      store.dispatch(
        finishApiCall({
          apiCallId: response.config.apiCallId,
        })
      );

      return normalizeSuccessResponse(response);
    },
    error => {
      store.dispatch(
        finishApiCall({
          apiCallId: error.config.apiCallId,
          error,
        })
      );

      if (error.response && error.response.status === 401) {
        store.dispatch(logout());
      }

      showErrorMessage(error);

      return normalizeErrorResponse(error);
    }
  );
}

function showErrorMessage(error) {
  const errorMsg =
    error.response && error.response.data
      ? error.response.data.message || error.response.data.error.inner
      : error;

  if (Array.isArray(errorMsg)) {
    AlertService.error(errorMsg.reduce((acc, curr) => `${acc}\n${curr}`, ''));
  } else {
    AlertService.error(`${errorMsg}`);
  }
}
{'fraction_non_alphanumeric': 0.14853556485355648, 'fraction_numerical': 0.0015690376569037657, 'mean_word_length': 2.883248730964467, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '1446974', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1131, 'n_words': 211}