Get axios Response object when error occurred with Redux Saga
Your code seems to be about right, only difference for me is this:
try { const response = yield call(apiCall, action.payload) yield put({ type: successReducer, payload: response }); } catch(error) { // Construct an error message let errorMessage = error.response.status + ":"+ error.response.statusText; yield put({ type: failReducer, payload: { message : errorMessage }}); }
So to me the difference seems to be that I access error.response.status insteadof error.request.response.
So it's an axios error coming to the catch as I expected.
I just follow the documentation to get the data :)
here is the part:
axios.get('/user/12345') .catch(function (error) { if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx console.log(error.response.data); console.log(error.response.status); console.log(error.response.headers); } else if (error.request) { // The request was made but no response was received // `error.request` is an instance of XMLHttpRequest in the browser and an instance of // http.ClientRequest in node.js console.log(error.request); } else { // Something happened in setting up the request that triggered an Error console.log('Error', error.message); } console.log(error.config); });
In my case I just need to get the message using:
error.response.data.message
Dude, I spent hours on it.
Here is what worked for me:
console.log(error.response.data.message);
So, inside your error
object, there is a response
object and then you can access everything inside the data
object.
try { const response = yield requestLogin(payload); console.log(`Response`, response);} catch (error) { yield put({ type: TYPES.LOGIN_FAILURE, error: error.response.data.message });}
You can debug it inside catch like this:
catch (error) { console.log('erro:', error.response.data.message) }