Move error types to wgpu-types

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
sagudev authored and Teodor Tanasoaia committed 2026-08-19 14:26:54 +02:00
1 parent c43d2207c0
commit 041f76bbb1
3 files changed
+132 -109

No files matched your search

+108 -1
View File
@@ -1,6 +1,10 @@
//! Shared types for WebGPU errors. See also:
//! <https://gpuweb.github.io/gpuweb/#errors-and-debugging>
use alloc::boxed::Box;
use alloc::string::String;
use core::{error, fmt};
/// A classification of WebGPU error for implementers of the WebGPU API to use in their own error
/// layer(s).
///
@@ -35,7 +39,110 @@ pub enum ErrorType {
///
/// This is intended to be used as a convenience by implementations of WebGPU to classify errors
/// returned by [`wgpu_core`](crate).
pub trait WebGpuError: core::error::Error + 'static {
pub trait WebGpuError: error::Error + 'static {
/// Determine the classification of this error as a WebGPU [`ErrorType`].
fn webgpu_error_type(&self) -> ErrorType;
}
/// The callback of [`uncaptured_error`](https://gpuweb.github.io/gpuweb/#eventdef-gpudevice-uncapturederror)
///
/// It must be a function with this signature.
pub trait UncapturedErrorHandler: Fn(Error) + Send + Sync + 'static {}
impl<T> UncapturedErrorHandler for T where T: Fn(Error) + Send + Sync + 'static {}
/// Kinds of [`Error`]s a [`push_error_scope`](https://gpuweb.github.io/gpuweb/#dom-gpudevice-pusherrorscope) may be configured to catch.
///
/// Corresponds to the [`GPUErrorFilter`] type in the WebGPU API.
///
/// [`GPUErrorFilter`]: https://gpuweb.github.io/gpuweb/#enumdef-gpuerrorfilter
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd)]
pub enum ErrorFilter {
/// Catch only out-of-memory errors.
OutOfMemory,
/// Catch only validation errors.
Validation,
/// Catch only internal errors.
Internal,
}
static_assertions::assert_impl_all!(ErrorFilter: Send, Sync);
/// Lower level source of the error.
///
/// `Send + Sync` varies depending on configuration.
#[cfg(any(
not(target_family = "wasm"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub type ErrorSource = Box<dyn error::Error + Send + Sync + 'static>;
/// Lower level source of the error.
///
/// `Send + Sync` varies depending on configuration.
#[cfg(not(any(
not(target_family = "wasm"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
)))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub type ErrorSource = Box<dyn error::Error + 'static>;
/// Errors resulting from usage of GPU APIs.
#[derive(Debug)]
pub enum Error {
/// Out of memory.
OutOfMemory {
/// Lower level source of the error.
source: ErrorSource,
},
/// Validation error, signifying a bug in code or data provided to `wgpu`.
Validation {
/// Lower level source of the error.
source: ErrorSource,
/// Description of the validation error.
description: String,
},
/// Internal error. Used for signalling any failures not explicitly expected by WebGPU.
///
/// These could be due to internal implementation or system limits being reached.
Internal {
/// Lower level source of the error.
source: ErrorSource,
/// Description of the internal GPU error.
description: String,
},
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::OutOfMemory { source } => Some(source.as_ref()),
Error::Validation { source, .. } => Some(source.as_ref()),
Error::Internal { source, .. } => Some(source.as_ref()),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::OutOfMemory { .. } => f.write_str("Out of Memory"),
Error::Validation { description, .. } => f.write_str(description),
Error::Internal { description, .. } => f.write_str(description),
}
}
}
impl WebGpuError for Error {
fn webgpu_error_type(&self) -> ErrorType {
match self {
Error::OutOfMemory { .. } => ErrorType::OutOfMemory,
Error::Validation { .. } => ErrorType::Validation,
Error::Internal { .. } => ErrorType::Internal,
}
}
}
+1 -83
View File
@@ -7,6 +7,7 @@ use crate::api::blas::{Blas, BlasGeometrySizeDescriptors, CreateBlasDescriptor};
use crate::api::tlas::{CreateTlasDescriptor, Tlas};
use crate::util::Mutex;
use crate::*;
pub use wgt::error::*;
/// Open connection to a graphics and/or compute device.
///
@@ -844,89 +845,6 @@ impl From<wgc::instance::RequestDeviceError> for RequestDeviceError {
}
}
/// The callback of [`Device::on_uncaptured_error()`].
///
/// It must be a function with this signature.
pub trait UncapturedErrorHandler: Fn(Error) + Send + Sync + 'static {}
impl<T> UncapturedErrorHandler for T where T: Fn(Error) + Send + Sync + 'static {}
/// Kinds of [`Error`]s a [`Device::push_error_scope()`] may be configured to catch.
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd)]
pub enum ErrorFilter {
/// Catch only out-of-memory errors.
OutOfMemory,
/// Catch only validation errors.
Validation,
/// Catch only internal errors.
Internal,
}
static_assertions::assert_impl_all!(ErrorFilter: Send, Sync);
/// Lower level source of the error.
///
/// `Send + Sync` varies depending on configuration.
#[cfg(send_sync)]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub type ErrorSource = Box<dyn error::Error + Send + Sync + 'static>;
/// Lower level source of the error.
///
/// `Send + Sync` varies depending on configuration.
#[cfg(not(send_sync))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub type ErrorSource = Box<dyn error::Error + 'static>;
/// Errors resulting from usage of GPU APIs.
///
/// By default, errors translate into panics. Depending on the backend and circumstances,
/// errors may occur synchronously or asynchronously. When errors need to be handled, use
/// [`Device::push_error_scope()`] or [`Device::on_uncaptured_error()`].
#[derive(Debug)]
pub enum Error {
/// Out of memory.
OutOfMemory {
/// Lower level source of the error.
source: ErrorSource,
},
/// Validation error, signifying a bug in code or data provided to `wgpu`.
Validation {
/// Lower level source of the error.
source: ErrorSource,
/// Description of the validation error.
description: String,
},
/// Internal error. Used for signalling any failures not explicitly expected by WebGPU.
///
/// These could be due to internal implementation or system limits being reached.
Internal {
/// Lower level source of the error.
source: ErrorSource,
/// Description of the internal GPU error.
description: String,
},
}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(Error: Send, Sync);
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::OutOfMemory { source } => Some(source.as_ref()),
Error::Validation { source, .. } => Some(source.as_ref()),
Error::Internal { source, .. } => Some(source.as_ref()),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::OutOfMemory { .. } => f.write_str("Out of Memory"),
Error::Validation { description, .. } => f.write_str(description),
Error::Internal { description, .. } => f.write_str(description),
}
}
}
/// Guard for an error scope pushed with [`Device::push_error_scope()`].
///
/// Call [`pop()`] to pop the scope and get a future for the result. If
+23 -25
View File
@@ -71,30 +71,28 @@ impl fmt::Debug for ContextWebGpu {
}
}
impl crate::Error {
fn from_js(js_error: js_sys::Object) -> Self {
let source = Box::<dyn core::error::Error + Send + Sync>::from("<WebGPU Error>");
if let Some(js_error) = js_error.dyn_ref::<webgpu_sys::GpuValidationError>() {
crate::Error::Validation {
source,
description: js_error.message(),
}
} else if let Some(js_error) = js_error.dyn_ref::<webgpu_sys::GpuInternalError>() {
crate::Error::Internal {
source,
description: js_error.message(),
}
} else if js_error.has_type::<webgpu_sys::GpuOutOfMemoryError>() {
crate::Error::OutOfMemory { source }
} else {
let constructor = js_error
.constructor()
.name()
.as_string()
.unwrap_or_default();
let value = js_error.to_string().as_string().unwrap_or_default();
panic!("Unexpected error: constructor={constructor} value={value}");
fn error_from_js(js_error: js_sys::Object) -> crate::Error {
let source = Box::<dyn core::error::Error + Send + Sync>::from("<WebGPU Error>");
if let Some(js_error) = js_error.dyn_ref::<webgpu_sys::GpuValidationError>() {
crate::Error::Validation {
source,
description: js_error.message(),
}
} else if let Some(js_error) = js_error.dyn_ref::<webgpu_sys::GpuInternalError>() {
crate::Error::Internal {
source,
description: js_error.message(),
}
} else if js_error.has_type::<webgpu_sys::GpuOutOfMemoryError>() {
crate::Error::OutOfMemory { source }
} else {
let constructor = js_error
.constructor()
.name()
.as_string()
.unwrap_or_default();
let value = js_error.to_string().as_string().unwrap_or_default();
panic!("Unexpected error: constructor={constructor} value={value}");
}
}
@@ -1075,7 +1073,7 @@ fn future_request_device(
fn future_pop_error_scope(
result: Result<js_sys::JsNullable<webgpu_sys::GpuError>, wasm_bindgen::JsValue>,
) -> Option<crate::Error> {
Some(crate::Error::from_js(result.ok()?.into_option()?.into()))
Some(error_from_js(result.ok()?.into_option()?.into()))
}
fn future_compilation_info(
@@ -2680,7 +2678,7 @@ impl dispatch::DeviceInterface for WebDevice {
fn on_uncaptured_error(&self, handler: Arc<dyn crate::UncapturedErrorHandler>) {
let f = Closure::wrap(Box::new(move |event: webgpu_sys::GpuUncapturedErrorEvent| {
let error = crate::Error::from_js(event.error().value_of());
let error = error_from_js(event.error().value_of());
handler(error);
}) as Box<dyn FnMut(_)>);
self.inner