Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

How to catch and handle a 422 from Axios in a Redux thunk

ErrorThe validation messages are visible in the Network tab but not in the app

Short answer

A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again. Axios rejects non-2xx responses and puts the body in error.response.data. In a thunk, return it with rejectWithValue so it reaches the reducer as the payload.

  • Applies to axios 1.x, Redux Toolkit 2, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the response body of the failing request in the Network tab. A 422 carries the reasons; the app must read that body.

    try {
      await axios.post("/api/users", data);
    } catch (error) {
      console.log(error.response?.status, error.response?.data);
    }
  2. 2Match what you see, then jump to its fix

    1. 1The UI shows only a generic 'Request failed with status code 422'The error is read from error.message instead of error.responseFix
    2. 2action.payload is undefined in the rejected case and action.error.message is 'Rejected'The thunk throws, so the reducer only sees a serialized errorFix
    3. 3dispatch(createUser(data)) resolves normally even when the server answered 422The component never receives the rejectionFix
    4. 4The same 422 appears on every retryThe request really is not processable and is retried unchangedFix

What does the error look like?

  • The validation messages are visible in the Network tab but not in the app
  • action.error.message is 'Rejected' and action.payload is undefined

Cause 1 of 4

The error is read from error.message instead of error.response

1Check

When the server answers with a status outside 2xx, Axios rejects, and the server's reply is in error.response, with data, status and headers. error.request means no reply came, and anything else means the request could not be set up.

grep -rn "error.message\|err.message" src

2Fix

Branch on the three cases and read error.response.data for the 422 body.

axios.post("/api/users", data).catch((error) => {
  if (error.response) {
    console.log(error.response.status, error.response.data);
  } else if (error.request) {
    console.log("No response received");
  } else {
    console.log("Error", error.message);
  }
});

3Verify

The validation messages from the server print in the console.

console.log(error.response?.data);

Confirmed by axios.rest

Did this fix it?

Cause 2 of 4

The thunk throws, so the reducer only sees a serialized error

1Check

If a thunk fails without rejectWithValue, a rejected action is dispatched with a serialized version of the error as action.error. The response body is not in it.

grep -rn "createAsyncThunk" src

2Fix

Return err.response.data with rejectWithValue, so it becomes action.payload.

export const createUser = createAsyncThunk(
  "users/create",
  async (data, { rejectWithValue }) => {
    try {
      const res = await axios.post("/api/users", data);
      return res.data;
    } catch (err) {
      return rejectWithValue(err.response?.data);
    }
  },
);

3Verify

The rejected action carries the server's messages in action.payload.

console.log(action.payload);

Confirmed by redux.js.org

Did this fix it?

Cause 3 of 4

The component never receives the rejection

1Check

A thunk never returns a rejected promise by itself: a failed request is a rejected action, not a thrown error, unless you unwrap it.

grep -rn "dispatch(createUser" src

2Fix

Call .unwrap() on the dispatched thunk. It throws the rejectWithValue payload, so a try/catch in the component can show the field errors.

const onSubmit = async (data) => {
  try {
    await dispatch(createUser(data)).unwrap();
  } catch (payload) {
    setFieldErrors(payload?.errors ?? {});
  }
};

3Verify

A deliberate invalid submit fills the field errors.

console.log('caught');

Confirmed by redux.js.org

Did this fix it?

Cause 4 of 4

The request really is not processable and is retried unchanged

1Check

MDN notes that clients receiving a 422 should expect that repeating the request without modification will fail again.

console.log(JSON.stringify(data));

2Fix

Do not retry. Show the server's messages next to the form fields, let the user correct the input, then send a changed request.

// show errors, do not auto-retry
setFieldErrors(error.response.data.errors);

3Verify

A corrected submit returns 2xx.

console.log(response.status);

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Standardise on one thunk shape: try/catch, rejectWithValue(err.response?.data).
  • Never retry a 422 automatically.
  • Validate on the client first so the common 422s never reach the server.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.