From 8290d92e2690ec9241140c985018c420f667f6f7 Mon Sep 17 00:00:00 2001 From: Kai Ninomiya Date: Thu, 17 Apr 2025 11:42:52 -0700 Subject: [PATCH] Show error dialog on unhandled rejection and uncaught exception These errors aren't supposed to happen in the samples, but if they do, we want them to be visible. Otherwise they can only be seen in the dev tools console, and sometimes you can't tell that the sample is misbehaving, e.g.: https://github.com/webgpu/webgpu-samples/pull/501#discussion_r2049424888 --- sample/util.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sample/util.ts b/sample/util.ts index 0dcd3856..0e047d4c 100644 --- a/sample/util.ts +++ b/sample/util.ts @@ -1,3 +1,14 @@ +// Show an error dialog if there's any uncaught exception or promise rejection. +// This gets set up on all pages that include util.ts. +window.addEventListener('unhandledrejection', (ev) => { + fail(`unhandled promise rejection, please report a bug! + https://github.com/webgpu/webgpu-samples/issues/new\n${ev.reason}`); +}); +window.addEventListener('error', (ev) => { + fail(`uncaught exception, please report a bug! + https://github.com/webgpu/webgpu-samples/issues/new\n${ev.error}`); +}); + /** Shows an error dialog if getting an adapter wasn't successful. */ export function quitIfAdapterNotAvailable( adapter: GPUAdapter | null