Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling when loading timeout + eslint rules #137

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
}
},
"rules": {
"prefer-promise-reject-errors": "error",
"no-throw-literal": "error",
"react/react-in-jsx-scope": "off",
"import/no-unresolved": "off"
},
Expand Down
9 changes: 8 additions & 1 deletion src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export async function waitFor(
predicate: () => Promise<boolean>,
interval: number,
_maxChecks: number,
rejectOnTimeout = true,
): Promise<void> {
// special case for 0 maxChecks (wait forever)
const maxChecks = _maxChecks === 0 ? Infinity : _maxChecks;
Expand All @@ -23,7 +24,11 @@ export async function waitFor(
checkCount++;
if (checkCount >= maxChecks) {
clearInterval(intervalId);
reject("Timed out waiting for condition");
if (rejectOnTimeout) {
reject(new Error("Timed out waiting for condition"));
} else {
resolve();
}
}
}
}, interval);
Expand All @@ -34,6 +39,7 @@ export async function waitTillStable(
getSize: () => Promise<number>,
interval: number,
timeout: number,
rejectOnTimeout = false, // default to assuming stable after timeout
): Promise<void> {
let lastSize = 0;
let countStableSizeIterations = 0;
Expand Down Expand Up @@ -61,6 +67,7 @@ export async function waitTillStable(
},
interval,
timeout / interval,
rejectOnTimeout,
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/pages/permission/requestPermission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export async function getUserPermission(): Promise<void> {

// Handling different error scenarios
if (error.name === "Permission denied") {
reject("MICROPHONE_PERMISSION_DENIED");
// TODO: catch this error and show a user-friendly message
reject(new Error("MICROPHONE_PERMISSION_DENIED"));
} else {
reject(error);
}
Expand Down
1 change: 1 addition & 0 deletions src/state/currentTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const createCurrentTaskSlice: MyStateCreator<CurrentTaskSlice> = (
},
200, // check every 200ms
100, // wait for up to 20 seconds (100*200ms)
false, // assume page fully loaded on timeout
);
}

Expand Down
Loading