Merge pull request #194 from bluewave-labs/fix/monitor-auth-slice-error-handling

Fixed error handling bug in redux slices
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-06-25 14:16:00 -04:00
commit 3cd6cbc62e
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";
}); });
}, },
}); });