<commit_before>import * as ActionCreators from './action-creators';
import * as ActionTypes from './action-types';
import {request, response, fail, requiresAuth, isFeedRequest, isFeedResponse} from './action-helpers';

//middleware for api requests
export const apiMiddleware = store => next => async (action) => {
  //ignore normal actions
  if (!action.apiRequest) {
    return next(action);
  }

  //dispatch request begin action
  //clean apiRequest to not get caught by this middleware
  var t = store.dispatch({...action, type: request(action.type), apiRequest: null});
  try {
    const apiResponse = await action.apiRequest(action.payload);
    const obj = await apiResponse.json();
    if (apiResponse.status === 200) {
      return store.dispatch({payload: obj, type: response(action.type), request: action.payload});
    } else if (apiResponse.status === 401) {
      return store.dispatch(ActionCreators.unauthenticated(obj));
    } else {
      return store.dispatch({payload: obj, type: fail(action.type), request: action.payload, response: apiResponse});
    }
  } catch (e) {
    return store.dispatch(ActionCreators.serverError(e));
  }
};<commit_msg>Handle network issues via sentry<commit_after>import * as ActionCreators from './action-creators';
import * as ActionTypes from './action-types';
import {request, response, fail, requiresAuth, isFeedRequest, isFeedResponse} from './action-helpers';

//middleware for api requests
export const apiMiddleware = store => next => async (action) => {
  //ignore normal actions
  if (!action.apiRequest) {
    return next(action);
  }

  //dispatch request begin action
  //clean apiRequest to not get caught by this middleware
  var t = store.dispatch({...action, type: request(action.type), apiRequest: null});
  try {
    const apiResponse = await action.apiRequest(action.payload);
    const obj = await apiResponse.json();
    if (apiResponse.status === 200) {
      return store.dispatch({payload: obj, type: response(action.type), request: action.payload});
    } else if (apiResponse.status === 401) {
      return store.dispatch(ActionCreators.unauthenticated(obj));
    } else {
      return store.dispatch({payload: obj, type: fail(action.type), request: action.payload, response: apiResponse});
    }
  } catch (e) {
    /*global Raven*/
    if (Raven) {
      Raven.captureException(e, { level: 'error', tags: { area: 'redux/apiMiddleware' }, extra: { action } });
    }
    return store.dispatch(ActionCreators.serverError(e));
  }
};
{'fraction_non_alphanumeric': 0.13705179282868526, 'fraction_numerical': 0.004780876494023904, 'mean_word_length': 3.9920477137176937, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '6167524', 'n_tokens_mistral': 717, 'n_tokens_neox': 704, 'n_words': 208}