Fixed error handling bug in redux slices

This commit is contained in:
Alex Holliday committed 2024-06-25 11:14:16 -07:00
1 parent 11c1819951
commit 8eae283507
2 files changed
+28 -11

No files matched your search

+12 -4
View File
@@ -18,7 +18,10 @@ export const register = createAsyncThunk(
const res = await axios.post(`${BASE_URL}/auth/register`, form); const res = await axios.post(`${BASE_URL}/auth/register`, form);
return res.data; return res.data;
} catch (error) { } catch (error) {
return thunkApi.rejectWithValue(error.response.data); if (error.response.data) {
return thunkApi.rejectWithValue(error.response.data);
}
return thunkApi.rejectWithValue(error.message);
} }
} }
); );
@@ -28,7 +31,10 @@ export const login = createAsyncThunk("auth/login", async (form, thunkApi) => {
const res = await axios.post(`${BASE_URL}/auth/login`, form); const res = await axios.post(`${BASE_URL}/auth/login`, form);
return res.data; return res.data;
} catch (error) { } 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);
} }
}); });
@@ -43,8 +49,10 @@ const handleAuthFulfilled = (state, action) => {
}; };
const handleAuthRejected = (state, action) => { const handleAuthRejected = (state, action) => {
state.isLoading = false; state.isLoading = false;
state.success = action.payload.success; state.success = false;
state.msg = action.payload.msg; state.msg = action.payload
? action.payload.msg
: "Failed to login or register";
}; };
const authSlice = createSlice({ const authSlice = createSlice({
+16 -7
View File
@@ -18,7 +18,10 @@ export const getMonitors = createAsyncThunk(
); );
return res.data; return res.data;
} catch (error) { } 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);
} }
} }
); );
@@ -38,7 +41,10 @@ export const getMonitorsByUserId = createAsyncThunk(
); );
return res.data; return res.data;
} catch (error) { } 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);
} }
} }
); );
@@ -70,8 +76,10 @@ const monitorsSlice = createSlice({
}) })
.addCase(getMonitors.rejected, (state, action) => { .addCase(getMonitors.rejected, (state, action) => {
state.isLoading = false; state.isLoading = false;
state.success = action.payload.success; state.success = false;
state.msg = action.payload.msg; state.msg = action.payload
? action.payload.msg
: "Getting montiors failed";
}) })
// ***************************************************** // *****************************************************
// Monitors by userId // Monitors by userId
@@ -86,10 +94,11 @@ const monitorsSlice = createSlice({
state.monitors = action.payload.data; state.monitors = action.payload.data;
}) })
.addCase(getMonitorsByUserId.rejected, (state, action) => { .addCase(getMonitorsByUserId.rejected, (state, action) => {
console.log(action);
state.isLoading = false; state.isLoading = false;
state.success = action.payload.success; state.success = false;
state.msg = action.payload.msg; state.msg = action.payload
? action.payload.msg
: "Getting montiors failed";
}); });
}, },
}); });