You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using createAsyncThunk to handle all my api resquests. However, when I'm doing the error handling in the rejected action, I can't seem to access the original body returned from the response.
exportconstaddContact=createAsyncThunk('contacts/addContactStatus',async(obj)=>{const{ data }=awaitapi.post('/contact/',obj);returndata;});constcontactsSlice=createSlice({name: 'contacts',
initialState,
reducers,extraReducers: {[addContact.pending]: (state)=>{state.loading=true;},[addContact.rejected]: (state,action)=>{state.loading=false;// I want to use error.response.data.message, which is how I normally // handle errors when using Axios directly,// But I can't seem to access itmessage.error(action.error.message);},[addContact.fulfilled]: (state,action)=>{message.success('Contato criado com sucesso');state.loading=false;state.entities.push(action.payload);state.selected.push(action.payload);},}
I really don't want to do the usual try/catch, since it kinda defeats the purpose of having the status action creators, which I think are great for making my code cleaner. I've looked through the past issues, but maybe I'm missing something. It would be great if someone could help me.
Thanks in advance!
The text was updated successfully, but these errors were encountered:
Hey @will-amaral , your error will be serialized via miniSerializeError unless you specifically catch and return it with rejectWithValue as shown here. There was a good amount of discussion about this behavior and why it was decided to use rejectWithValue that you can skim through here: #390
In your example, you'd need to do this:
exportconstaddContact=createAsyncThunk('contacts/addContactStatus',async(obj,{ rejectWithValue })=>{try{const{ data }=awaitapi.post('/contact/',obj);returndata;}catch(err){returnrejectWithValue(err.response.data);// or whatever path you normally reference});
Yep. createAsyncThunk defaults to the simplest approach, which is the entire successful return value becomes payload, and the entire thrown error gets serialized and included. If you need more specific details, it's up to you to parse and include them in the rejection yourself.
I'm using
createAsyncThunk
to handle all my api resquests. However, when I'm doing the error handling in the rejected action, I can't seem to access the original body returned from the response.I really don't want to do the usual try/catch, since it kinda defeats the purpose of having the status action creators, which I think are great for making my code cleaner. I've looked through the past issues, but maybe I'm missing something. It would be great if someone could help me.
Thanks in advance!
The text was updated successfully, but these errors were encountered: