svrcekmichal / redux-axios-middleware

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

response interceptor not working for response with 400, 500 status code #27

Closed nindavidw closed 8 years ago

nindavidw commented 8 years ago

Hello @svrcekmichal

I've confirmed that response interceptor is not working when the response's httpStatus code is not 200. Otherwise it seems like working with 200 status codes. Would you please check this?

Thanks, Sergey

nindavidw commented 8 years ago

What i am trying to do is that dispatch action(for showing notification UI) when request is failed. (get 400 or 500 response) I think i can achieve this goal by using response interceptor?

onError callback function in option can be solution, but i don't know how to dispatch action in onError callback.

Note that I am trying to show global notification UI for all requests failure.

nmaves commented 8 years ago

I think @svrcekmichal latest commit for #26 now passes the dispatch method to the interceptors. Can you try that and let us know if that works?

svrcekmichal commented 8 years ago

Hi @SergeyKorchevskiy you can use interceptor two ways:

  1. as function If you use interceptor as function it will only be called on success response
  2. as {success: func, error: func} If you define single interceptor as object with success and error keys, it will call specific one according success or error of request

Please try it the second way, and let us know if it didn't work. I will try to create some tests tomorrow and commit them to package to better show usage

nindavidw commented 8 years ago

@svrcekmichal Thanks for your hints. I tried 2nd way, but seems like its not working.

I finally found the solution like below by myself.

const client = axios.create({ 
  baseURL:'http://localhost:8080/api',
  responseType: 'json'
});

const clientOptions = {
  interceptors: {
    request: [({getState, dispatch, action}, config) => {
      console.log('request interceptor working');
      return config;
    }],
    response: [({getState, dispatch, action}, response) => {
      console.log('response interceptor working only for 200')
      return response;
    }]
  },
  onError: ({ action, next, error, getState, dispatch }, actionOptions) => {
    console.log('400,500 response goes here');
    console.log('i even can dispatch action in here');
  }
}

const store = createStore(
  ...reducers,
  applyMiddleware(
    axiosMiddleware(client, clientOptions)
  )
)

I can dispatch action for showing error notification in onError callback. Since this solves my problem, i am going to close this issue.

Thanks!

svrcekmichal commented 8 years ago

Well, you can override onError but it will change default logic of middleware. It will not dispatch original error action and return promise and it will even break handling of fatal errors in axios. I can't forbide you to do that, but in my opinion if you have some time left, I will look to this issue in about 12hour and I will write tests too

nindavidw commented 8 years ago

@svrcekmichal Okay, I will wait for your solution.

svrcekmichal commented 8 years ago

@SergeyKorchevskiy Hi, little bit late, but I have created tests for interceptors and they seem to work fine. I have added them in branch feature/tests in this file. Please give it a look and if you don't make it work, please send you code and I can check if you don't have any typo or another mistake

svrcekmichal commented 8 years ago

@SergeyKorchevskiy closing for know, if you still have issue just write and i will reopen this

llopez95 commented 7 years ago

Hi @svrcekmichal,

Could you provide an example of implementation with OAuth2 ? I'm stuck with it for few days now and it drives me nuts!! Thanks... :/

webattitude commented 5 years ago

Hi, is http status code available somewhere in case of interceptor error handler ?

matviishyn commented 4 years ago

Check my answer in https://github.com/svrcekmichal/redux-axios-middleware/issues/87