From c7e6170fd03d3b0cbaafea1bd4515b86e6f33499 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Fri, 13 Jun 2025 15:44:54 +0900 Subject: [PATCH] refactor(deno): use `WebGpuError` for error classification --- CHANGELOG.md | 2 +- deno_webgpu/error.rs | 106 +++++++++++++++---------------------------- 2 files changed, 38 insertions(+), 70 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e971fa283..71dca8cfd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -110,7 +110,7 @@ Bottom level categories: ### Changes - Loosen Viewport validation requirements to match the [new specs](https://github.com/gpuweb/gpuweb/pull/5025). By @ebbdrop in [#7564](https://github.com/gfx-rs/wgpu/pull/7564) -- `wgpu` now uses `wgpu-types::error::WebGpuError` to classify errors. Any changes here are likely to be regressions; please report them if you find them! By @ErichDonGubler in [#6547](https://github.com/gfx-rs/wgpu/pull/6547). +- `wgpu` and `deno_webgpu` now use `wgpu-types::error::WebGpuError` to classify errors. Any changes here are likely to be regressions; please report them if you find them! By @ErichDonGubler in [#6547](https://github.com/gfx-rs/wgpu/pull/6547). #### General diff --git a/deno_webgpu/error.rs b/deno_webgpu/error.rs index ba2827bed..6831d93aa 100644 --- a/deno_webgpu/error.rs +++ b/deno_webgpu/error.rs @@ -36,6 +36,7 @@ use wgpu_core::resource::CreateQuerySetError; use wgpu_core::resource::CreateSamplerError; use wgpu_core::resource::CreateTextureError; use wgpu_core::resource::CreateTextureViewError; +use wgpu_types::error::{ErrorType, WebGpuError}; use crate::device::GPUDeviceLostInfo; use crate::device::GPUDeviceLostReason; @@ -183,6 +184,17 @@ impl Display for GPUError { impl std::error::Error for GPUError {} +impl GPUError { + fn from_webgpu(e: impl WebGpuError) -> Self { + match e.webgpu_error_type() { + ErrorType::Internal => GPUError::Internal, + ErrorType::DeviceLost { reason } => GPUError::Lost(reason.into()), + ErrorType::OutOfMemory => GPUError::OutOfMemory, + ErrorType::Validation => GPUError::Validation(fmt_err(&e)), + } + } +} + fn fmt_err(err: &(dyn std::error::Error + 'static)) -> String { let mut output = err.to_string(); @@ -201,7 +213,7 @@ fn fmt_err(err: &(dyn std::error::Error + 'static)) -> String { impl From for GPUError { fn from(err: EncoderStateError) -> Self { - GPUError::Validation(fmt_err(&err)) + GPUError::from_webgpu(err) } } @@ -213,188 +225,144 @@ impl From for GPUError { impl From for GPUError { fn from(err: CreateBufferError) -> Self { - match err { - CreateBufferError::Device(err) => err.into(), - CreateBufferError::AccessError(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: DeviceError) -> Self { - match err { - DeviceError::Lost => GPUError::Lost(GPUDeviceLostReason::Unknown), - DeviceError::OutOfMemory => GPUError::OutOfMemory, - _ => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: BufferAccessError) -> Self { - match err { - BufferAccessError::Device(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateBindGroupLayoutError) -> Self { - match err { - CreateBindGroupLayoutError::Device(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreatePipelineLayoutError) -> Self { - match err { - CreatePipelineLayoutError::Device(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateBindGroupError) -> Self { - match err { - CreateBindGroupError::Device(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: RenderBundleError) -> Self { - GPUError::Validation(fmt_err(&err)) + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateRenderBundleError) -> Self { - GPUError::Validation(fmt_err(&err)) + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CommandEncoderError) -> Self { - GPUError::Validation(fmt_err(&err)) + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: QueryError) -> Self { - GPUError::Validation(fmt_err(&err)) + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: ComputePassError) -> Self { - GPUError::Validation(fmt_err(&err)) + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateComputePipelineError) -> Self { - match err { - CreateComputePipelineError::Device(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: GetBindGroupLayoutError) -> Self { - GPUError::Validation(fmt_err(&err)) + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateRenderPipelineError) -> Self { - match err { - CreateRenderPipelineError::Device(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: RenderPassError) -> Self { - GPUError::Validation(fmt_err(&err)) + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateSamplerError) -> Self { - match err { - CreateSamplerError::Device(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateShaderModuleError) -> Self { - match err { - CreateShaderModuleError::Device(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateTextureError) -> Self { - match err { - CreateTextureError::Device(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateTextureViewError) -> Self { - GPUError::Validation(fmt_err(&err)) + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateQuerySetError) -> Self { - match err { - CreateQuerySetError::Device(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: QueueSubmitError) -> Self { - match err { - QueueSubmitError::Queue(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: QueueWriteError) -> Self { - match err { - QueueWriteError::Queue(err) => err.into(), - err => GPUError::Validation(fmt_err(&err)), - } + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: ClearError) -> Self { - GPUError::Validation(fmt_err(&err)) + GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: ConfigureSurfaceError) -> Self { - GPUError::Validation(fmt_err(&err)) + GPUError::from_webgpu(err) } }