Skip to content

Commit

Permalink
Improve error handling when loading timeout + eslint rules (#137)
Browse files Browse the repository at this point in the history
* fix: improve timeout handling

* style: no-throw-literal

* style: add eslint rule prefer-promise-reject-errors
  • Loading branch information
mondaychen authored Apr 23, 2024
1 parent abfc501 commit 0386568
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
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

0 comments on commit 0386568

Please sign in to comment.