Skip to content

Commit f0d200d

Browse files
committed
Reland "Show error dialog on unhandled rejection and uncaught exception (#502)" (#509)
Reland with fix. Reverted in #509. Original in #502.
1 parent 2fa0137 commit f0d200d

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

sample/util.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
// Show an error dialog if there's any uncaught exception or promise rejection.
2+
// This gets set up on all pages that include util.ts.
3+
globalThis.addEventListener('unhandledrejection', (ev) => {
4+
fail(`unhandled promise rejection, please report a bug!
5+
https://github.com/webgpu/webgpu-samples/issues/new\n${ev.reason}`);
6+
});
7+
globalThis.addEventListener('error', (ev) => {
8+
fail(`uncaught exception, please report a bug!
9+
https://github.com/webgpu/webgpu-samples/issues/new\n${ev.error}`);
10+
});
11+
112
/** Shows an error dialog if getting an adapter wasn't successful. */
213
export function quitIfAdapterNotAvailable(
314
adapter: GPUAdapter | null
@@ -40,19 +51,18 @@ export function quitIfWebGPUNotAvailable(
4051
if (!device) {
4152
quitIfAdapterNotAvailable(adapter);
4253
fail('Unable to get a device for an unknown reason');
43-
return;
4454
}
4555

4656
device.lost.then((reason) => {
4757
fail(`Device lost ("${reason.reason}"):\n${reason.message}`);
4858
});
49-
device.addEventListener('uncapturederror', (ev) => {
59+
device.addEventListener('uncapturederror', (ev: GPUUncapturedErrorEvent) => {
5060
fail(`Uncaptured error:\n${ev.error.message}`);
5161
});
5262
}
5363

5464
/** Fail by showing a console error, and dialog box if possible. */
55-
const fail = (() => {
65+
const fail: (message: string) => never = (() => {
5666
type ErrorOutput = { show(msg: string): void };
5767

5868
function createErrorOutput() {

0 commit comments

Comments
 (0)