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
2024-06-25 14:16:00 -04:00
committed by GitHub
2 changed files with 28 additions and 11 deletions

View File

@@ -18,7 +18,10 @@ export const register = createAsyncThunk(
const res = await axios.post(`${BASE_URL}/auth/register`, form);
return res.data;
} 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);
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);
}
});
@@ -43,8 +49,10 @@ const handleAuthFulfilled = (state, action) => {
};
const handleAuthRejected = (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to login or register";
};
const authSlice = createSlice({

View File

@@ -18,7 +18,10 @@ export const getMonitors = 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);
}
}
);
@@ -38,7 +41,10 @@ export const getMonitorsByUserId = 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);
}
}
);
@@ -70,8 +76,10 @@ const monitorsSlice = createSlice({
})
.addCase(getMonitors.rejected, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Getting montiors failed";
})
// *****************************************************
// Monitors by userId
@@ -86,10 +94,11 @@ const monitorsSlice = createSlice({
state.monitors = action.payload.data;
})
.addCase(getMonitorsByUserId.rejected, (state, action) => {
console.log(action);
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Getting montiors failed";
});
},
});