I use axios in my response / redux application, and when I get errors like 401, 404, etc. I have to deal with them for every action function when I make calls in axios. I have axios_config.js where I wrapped axios calls with some common idioms. For example:
// need to move this to app config const BASE_URL = 'http://localhost:8080/api/'; function config() { return { headers: {'X-Token-Auth': localStorage.getItem('token')} } } export function fetchData(url) { return axios.get(`${BASE_URL}${url}`, config()); };
Where I'm afraid, these are common mistakes like 401, 404, etc. I am currently doing this:
export function fetchBrands() { return function(dispatch) { dispatch({type:FETCHING_BRANDS}); fetchData('brands') .then(response => { dispatch({ type: FETCH_BRANDS_SUCCESS, payload: response }); }) .catch(err => {
But in the catch I don't want to deal with 401, 404, etc. everytime. Therefore, I need to deal with those who work on a more global scale, but are still able to handle certain errors for the request, for example, server-side validation errors.
reactjs redux axios react-redux
Gregg
source share