How to get axios error response INTO the redux saga catch method
See original GitHub issueThe code with axios is:
export const createBlaBla = (payload) => {
return axios.post('/some-url', payload)
.then(response => response)
.catch(err => err);
}
And then I’m using this with redux-saga like this:
function* createBlaBlaFlow(action) {
try {
const response = yield call(createBlaBla, action.payload);
if (response) {
yield put({
type: CREATE_BLA_BLA_SUCCESS
});
}
} catch (err) {
// I need the error data here ..
yield put({
type: CREATE_BLA_BLA_FAILURE,
payload: 'failed to create bla-bla'
});
}
}
In case of some error on the backend - like invalid data send to the backend - it returns a 400 response with some useful data:
{
"code":"ERR-1000",
"message":"Validation failed because ..."
"method":"POST",
"errorDetails":"..."
}
But I don’t receive this useful data in the catch statement inside the saga. I can console.log() the data in the axios catch statement, also I can get it inside the try statement in the saga, but it never arrives in the catch.
Probably I need to do something else? … Or the server shouldn’t return 400 response in this case?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:12 (2 by maintainers)
Top Results From Across the Web
How to get axios error response INTO the ... - Stack Overflow
So, I came up with two solutions of this problem. === First one - very dump workaround, but actually it can be handy...
Read more >How to handle axios error in redux-saga in try catch - Martin l.k
The problem. My original code is as following: I have a fetch function in my /services/Api.js: import axios from axios ...
Read more >Error Handling | Redux-Saga
We can catch errors inside the Saga using the familiar try/catch syntax. import Api from './path/to/api'
Read more >Get axios Response object when error occurred with Redux ...
So, inside your error object, there is a response object and then you can access everything inside the data object. try { const...
Read more >Data fetching with Redux and Axios - LogRocket Blog
payload; const dataOrParams = ["GET", "DELETE"].includes(method) ? "params" ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You are catching errors in your
createBlaBla
, so the error does not bubble to the generator. Just replace youcreateBlaBla
as follows:@rajat-np I am trying that and it isn’t working for me? Could I have some clarity on this please?