From fe725d6df0d93934260d75f00a76c68113952ee8 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 7 Apr 2026 18:18:35 +0200 Subject: [PATCH] Return helpful error when WebGPU is disabled (#9319) --- wgpu/src/backend/webgpu.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/wgpu/src/backend/webgpu.rs b/wgpu/src/backend/webgpu.rs index 65f6ba9da..060868f2a 100644 --- a/wgpu/src/backend/webgpu.rs +++ b/wgpu/src/backend/webgpu.rs @@ -1120,11 +1120,19 @@ impl ContextWebGpu { } }; - // Not returning this error because it is a type error that shouldn't happen unless - // the browser, JS builtin objects, or wasm bindings are misbehaving somehow. - let context: webgpu_sys::GpuCanvasContext = context - .dyn_into() - .expect("canvas context is not a GPUCanvasContext"); + // An error here indicated that WebGPU is disabled in the browser. + let context: webgpu_sys::GpuCanvasContext = + context + .dyn_into() + .map_err(|actual| crate::CreateSurfaceError { + inner: crate::CreateSurfaceErrorKind::Web(format!( + "`canvas.getContext()` returned a value that did not coerce to \ + `GPUCanvasContext`. \ + This is likely because WebGPU is disabled in this browser. \ + Expected: `GPUCanvasContext`, Actual: `{}`", + actual.to_string() + )), + })?; Ok(WebSurface { gpu: self.gpu.clone(),