I am trying to handle a Unauthorized error from a server using redux-saga. This is my saga:
function* logIn(action) { try { const user = yield call(Api.logIn, action); yield put({type: types.LOG_IN_SUCCEEDED, user}); } catch (error) { yield put({type: types.LOG_IN_FAILED, error}); } }
I retrieve the data as follows:
fetchUser(action) { const {username, password} = action.user; const body = {username, password}; return fetch(LOGIN_URL, { method, headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(body) }) .then(res => { res.json().then(json => { if (res.status >= 200 && res.status < 300) { return json } else { throw res } }) }) .catch(error => {throw error}); }
But in any case, the result is {type: 'LOG_IN_SUCCEEDED', user: undefined} when I expect {type: 'LOG_IN_FAILED', error: 'Unauthorized'} . Where is my mistake? How to handle errors correctly with Redux-Saga?
rel1x source share