Skip to content

Commit

Permalink
Add hardening against DB error
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding committed Jul 16, 2023
1 parent 1102ab8 commit 707a351
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/features/settings/settingsSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ export const appearanceSlice = createSlice({
...initialState,
ready: true,
}),

settingsReady: (state) => {
state.ready = true;
},
},
});

Expand Down Expand Up @@ -223,7 +227,7 @@ export const getBlurNsfw =
export const fetchSettingsFromDatabase = createAsyncThunk<SettingsState>(
"appearance/fetchSettingsFromDatabase",
async (_, thunkApi) => {
return db.transaction("r", db.settings, async () => {
const result = db.transaction("r", db.settings, async () => {
const state = thunkApi.getState() as RootState;
const collapse_comment_threads = await db.getSetting(
"collapse_comment_threads"
Expand Down Expand Up @@ -266,6 +270,15 @@ export const fetchSettingsFromDatabase = createAsyncThunk<SettingsState>(
},
};
});

try {
return await result;
} catch (error) {
// In the event of a database error, attempt to render the UI anyways
thunkApi.dispatch(settingsReady());

throw error;
}
}
);

Expand All @@ -280,6 +293,7 @@ export const {
setUserDarkMode,
setUseSystemDarkMode,
setDefaultCommentSort,
settingsReady,
} = appearanceSlice.actions;

export default appearanceSlice.reducer;

0 comments on commit 707a351

Please sign in to comment.