From 91f2cac4383b7aa8c0778478e4ade184fa909c65 Mon Sep 17 00:00:00 2001 From: Daniel Cojocea Date: Tue, 25 Jun 2024 14:20:13 -0400 Subject: [PATCH] Fixed error handling bug --- Client/src/Features/Auth/authSlice.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Client/src/Features/Auth/authSlice.js b/Client/src/Features/Auth/authSlice.js index e471b6307..cf808b431 100644 --- a/Client/src/Features/Auth/authSlice.js +++ b/Client/src/Features/Auth/authSlice.js @@ -55,7 +55,10 @@ export const update = createAsyncThunk( }); return res.data; } catch (error) { - return thunkApi.rejectWithValue(error.response.data); + if (error.response && error.response.data) { + return thunkApi.rejectWithValue(error.response.data); + } + return thunkApi.rejectWithValue(error.message); } } ); @@ -90,7 +93,14 @@ const handleUpdateFulfilled = (state, action) => { state.success = action.payload.success; state.msg = action.payload.msg; state.user = action.payload.data; -} +}; +const handleUpdateRejected = (state, action) => { + state.isLoading = false; + state.success = false; + state.msg = action.payload + ? action.payload.msg + : "Failed to update profile data."; +}; const authSlice = createSlice({ name: "auth", @@ -123,7 +133,7 @@ const authSlice = createSlice({ state.isLoading = true; }) .addCase(update.fulfilled, handleUpdateFulfilled) - .addCase(update.rejected, handleAuthRejected); + .addCase(update.rejected, handleUpdateRejected); }, });