feat(types): add error::{ErrorType, WebGpuError} APIs

This commit is contained in:
Erich Gubler
2024-11-14 14:50:01 -05:00
parent c3922d53d7
commit c8752e5118
3 changed files with 50 additions and 0 deletions

46
wgpu-types/src/error.rs Normal file
View File

@@ -0,0 +1,46 @@
//! Shared types for WebGPU errors. See also:
//! <https://gpuweb.github.io/gpuweb/#errors-and-debugging>
use crate::DeviceLostReason;
/// A classification of WebGPU error for implementers of the WebGPU API to use in their own error
/// layer(s).
///
/// Strongly correlates to the [`GPUError`] and [`GPUErrorFilter`] types in the WebGPU API, with an
/// additional [`Self::DeviceLost`] variant.
///
/// [`GPUError`]: https://gpuweb.github.io/gpuweb/#gpuerror
/// [`GPUErrorFilter`]: https://gpuweb.github.io/gpuweb/#enumdef-gpuerrorfilter
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ErrorType {
/// A [`GPUInternalError`].
///
/// [`GPUInternalError`]: https://gpuweb.github.io/gpuweb/#gpuinternalerror
Internal,
/// A [`GPUOutOfMemoryError`].
///
/// [`GPUOutOfMemoryError`]: https://gpuweb.github.io/gpuweb/#gpuoutofmemoryerror
OutOfMemory,
/// A [`GPUValidationError`].
///
/// [`GPUValidationError`]: https://gpuweb.github.io/gpuweb/#gpuvalidationerror
Validation,
/// Indicates that device loss occurred. In JavaScript, this means the [`GPUDevice.lost`]
/// property should be `resolve`d.
///
/// [`GPUDevice.lost`]: https://www.w3.org/TR/webgpu/#dom-gpudevice-lost
DeviceLost {
/// The reason the device was lost.
reason: DeviceLostReason,
},
}
/// A trait for querying the [`ErrorType`] classification of an error.
///
/// 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 {
/// Determine the classification of this error as a WebGPU [`ErrorType`].
fn webgpu_error_type(&self) -> ErrorType;
}

View File

@@ -36,6 +36,7 @@ pub mod assertions;
mod cast_utils;
mod counters;
mod env;
pub mod error;
mod features;
pub mod instance;
pub mod math;