A
A
Artemmmm132021-01-20 13:53:36
React
Artemmmm13, 2021-01-20 13:53:36

How to handle c api error in redux thunk?

There is an api written in Laravel that returns a response with errors and with the corresponding code:
60080a3aef224886476728.png
I send a request to the server in redux thunk and I want to return an error to the place from which dispatch was called

//Thunk
export const login = ({email,password,rememberMe}) => async(dispatch) => {
  try{
    dispatch(setLoading(true));
    let response = await authapi.login(email, password, rememberMe);
    if(response.data.status === 1){
      //Какие то действия
    }
  }catch(e){
                //Хочу обработать ошибку здесь ,но ошибка === undefined
    console.log('login error', e);
  }
}

In the catch block, I get undefined, although if I look through the Network tab, the error comes up with the right message, but in the console I get the following:
60080b572cf50781243227.png
How can I catch this error and return it from thunk ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2021-01-20
@abberati

Login in the code, and logout in the screenshot. Why?
In the same place, an error is displayed on the screenshot, which means it is not undefined.
Process it like this:

} catch(e) {
    dispatch({ type: ERROR_TYPE, payload: e.message })
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question