Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nindavidw opened this issue Jun 28, 2016 · 11 comments
Closed

Comments

@nindavidw
Copy link
Contributor

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
Copy link
Contributor Author

nindavidw commented Jun 28, 2016

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
Copy link
Collaborator

nmaves commented Jun 28, 2016

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
Copy link
Owner

svrcekmichal commented Jun 28, 2016

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
Copy link
Contributor Author

@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
Copy link
Owner

svrcekmichal commented Jun 28, 2016

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
Copy link
Contributor Author

@svrcekmichal Okay, I will wait for your solution.

@nindavidw nindavidw reopened this Jun 29, 2016
@svrcekmichal
Copy link
Owner

@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
Copy link
Owner

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

@llopez95
Copy link

llopez95 commented Nov 16, 2016

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
Copy link

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

@matviishyn
Copy link

Check my answer in #87

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants