svrcekmichal / redux-axios-middleware

Redux middleware for fetching data with axios HTTP client
MIT License
920 stars 96 forks source link

Intercept response error without handling it #55

Closed reederz closed 7 years ago

reederz commented 7 years ago

I would like to be able to intercept a response error with a response error interceptor and still get the ${requestAction}_FAIL action to be triggered. But it seems that the error just gets swallowed up whenever I add such an interceptor. Am I missing something?

For the moment, I'm just using onError handler like this:

const middlewareConfig = {
  onError: ({ action, error, next, dispatch }) => {
    if (error.response.status === 401) {
      dispatch(AuthActions.logout());
    }
    // propagate failure for further handling
    const nextAction = {
      type: `${action.type}_FAIL`,
      error,
      meta: {
        previousAction: action,
      },
    };
    next(nextAction);
    return nextAction;
  },
};
huytbt commented 7 years ago

@reederz I think you should use like this:

const middlewareConfig = {
  onError: ({ action, error, next, dispatch }) => {
    if (error.response.status === 401) {
      return dispatch(AuthActions.logout());
    }
    let nextAction = {
      error,
      meta: {
        previousAction: action,
      },
    };
    if (action.types && action.types.length === 3) {
      nextAction.type = action.types[2];
    } else {
      nextAction.type = action.type + '_FAIL';
    }
    next(nextAction);
    return nextAction;
  }
};
reederz commented 7 years ago

@huytbt thanks. Care to ellaborate on your improvement upon my snippet?

In any case, both of these snippets seem like workarounds. To me, it seems like it would be better handled with a response error interceptor. We would just need an option for NOT handling an error but, instead, propagating it so that it could be handled by reducers.

nmaves commented 7 years ago

I am going to close this issue as there as been no interaction for over a month. If this still an issue please reopen the issue.