Axios: 422 response not rejected

Created on 22 Feb 2017  ·  3Comments  ·  Source: axios/axios

We have a project that uses [email protected] (upgrading to 0.15.3 did not resolve the issue) in many parts of the application. In all of the other parts of the app, we're able to .catch() 422 response codes. Recently we noticed that 1 request is resolving a promise that gets a 422 response. In all other parts of our application, a 422 causes axios promises to be rejected.

export function addTool(name: string): Thunk {
  return (dispatch, getState) => {
    Axios
      .post<Tool>(API.current.toolsPath, { name })
      .then(resp => {
        if (resp instanceof Error) { throw resp; }
        success("Tool has been saved.", "Success");
        dispatch(addToolOk(resp.data));
      })
      .catch((e: Error) => {
        dispatch(addToolNo(e));
        error(prettyPrintApiErrors(e));
      });
  };
}

Source code here

I was able to work around the issue by adding the following line to my .then():

if (resp instanceof Error) { throw resp; }

Which works fine, but I'm still confused as to why this particular 422 is resolved (rather than rejected). We also have some interceptors in use, but they haven't cause any issues for the other requests we make.

Are there any circumstances in that would cause axios to resolve a 422 rather than reject it? Please let me know if you need any other information.

Thanks for the help!

Most helpful comment

After investigating this issue, I found that it was actually a local type error hidden deep in the call stack somewhere, leading me to believe that it is a local issue rather than a problem with Axios.

UPDATE:

After some investigation, I was able to pin this down to a bad "response rejected" interceptor.

Within the interceptor I wrote:

return error

and I solved the problem by instead writing:

return Promise.reject(error);

All 3 comments

Confirmed. I'm also seeing something similar when using axios with redux-saga where some 422's aren't being handled as a rejected promise.

Never mind, found my problem. Interceptors weren't properly propagating errors down to the app layer, d'oh! 😳

After investigating this issue, I found that it was actually a local type error hidden deep in the call stack somewhere, leading me to believe that it is a local issue rather than a problem with Axios.

UPDATE:

After some investigation, I was able to pin this down to a bad "response rejected" interceptor.

Within the interceptor I wrote:

return error

and I solved the problem by instead writing:

return Promise.reject(error);
Was this page helpful?
0 / 5 - 0 ratings