How to catch and handle a 422 from Axios in a Redux thunk
The validation messages are visible in the Network tab but not in the appShort 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.
Fix it fast
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); }2Match what you see, then jump to its fix
- 1
The UI shows only a generic 'Request failed with status code 422'The error is read from error.message instead of error.responseFix - 2
action.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
dispatch(createUser(data)) resolves normally even when the server answered 422The component never receives the rejectionFix - 4
The same 422 appears on every retryThe request really is not processable and is retried unchangedFix
- 1
What does the error look like?
The validation messages are visible in the Network tab but not in the appaction.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" src2Fix
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
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" src2Fix
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
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" src2Fix
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
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix