// Copyright 2018-2025 the Deno authors. MIT license. use std::fmt::Display; use std::fmt::Formatter; use std::sync::Arc; use deno_core::v8; use deno_core::JsRuntime; use wgpu_core::binding_model::CreateBindGroupError; use wgpu_core::binding_model::CreateBindGroupLayoutError; use wgpu_core::binding_model::CreatePipelineLayoutError; use wgpu_core::binding_model::GetBindGroupLayoutError; use wgpu_core::command::ClearError; use wgpu_core::command::CommandEncoderError; use wgpu_core::command::ComputePassError; use wgpu_core::command::CreateRenderBundleError; use wgpu_core::command::EncoderStateError; use wgpu_core::command::PassStateError; use wgpu_core::command::QueryError; use wgpu_core::command::RenderBundleError; use wgpu_core::command::RenderPassError; use wgpu_core::device::queue::QueueSubmitError; use wgpu_core::device::queue::QueueWriteError; use wgpu_core::device::Device; use wgpu_core::device::DeviceError; use wgpu_core::pipeline::CreatePipelineError; use wgpu_core::pipeline::CreateShaderModuleError; use wgpu_core::present::ConfigureSurfaceError; use wgpu_core::resource::BufferAccessError; use wgpu_core::resource::CreateBufferError; use wgpu_core::resource::CreateQuerySetError; use wgpu_core::resource::CreateSamplerError; use wgpu_core::resource::CreateTextureError; use wgpu_core::resource::CreateTextureViewError; use wgpu_types::error::ErrorFilter; use wgpu_types::error::{ErrorType, WebGpuError}; pub type ErrorHandler = std::rc::Rc; pub struct DeviceErrorHandler { device: Arc, } impl DeviceErrorHandler { pub fn new(device: Arc) -> Self { Self { device } } pub fn push_error>(&self, err: Option) { let Some(err) = err else { return; }; let err = err.into(); self.device.handle_error_nolabel(err, ""); } } #[derive(deno_core::WebIDL, Eq, PartialEq)] #[webidl(enum)] pub enum GPUErrorFilter { Validation, OutOfMemory, Internal, } impl From for ErrorFilter { fn from(filter: GPUErrorFilter) -> Self { match filter { GPUErrorFilter::Validation => ErrorFilter::Validation, GPUErrorFilter::OutOfMemory => ErrorFilter::OutOfMemory, GPUErrorFilter::Internal => ErrorFilter::Internal, } } } #[derive(Debug, deno_error::JsError)] pub enum GPUError { // TODO(@crowlKats): consider adding an unreachable value that uses unreachable!() #[class("UNREACHABLE")] Lost, #[class("GPUValidationError")] Validation(String), #[class("GPUOutOfMemoryError")] OutOfMemory, #[class("GPUInternalError")] Internal, } impl Display for GPUError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { GPUError::Lost => Ok(()), GPUError::Validation(s) => f.write_str(s), GPUError::OutOfMemory => f.write_str("not enough memory left"), GPUError::Internal => Ok(()), } } } impl std::error::Error for GPUError {} impl GPUError { pub(crate) fn from_webgpu(e: impl WebGpuError) -> Self { match e.webgpu_error_type() { ErrorType::Internal => GPUError::Internal, ErrorType::DeviceLost => GPUError::Lost, // this will be ignored by handle_error in wgpu-core ErrorType::OutOfMemory => GPUError::OutOfMemory, ErrorType::Validation => GPUError::Validation(fmt_err(&e)), } } } impl From for GPUError { fn from(err: wgpu_types::error::Error) -> Self { match err { wgpu_types::error::Error::Validation { description, source, } => { if let Some(e) = source.source() { GPUError::Validation(fmt_err(e)) } else { GPUError::Validation(description) } } wgpu_types::error::Error::OutOfMemory { .. } => GPUError::OutOfMemory, wgpu_types::error::Error::Internal { .. } => GPUError::Internal, } } } impl WebGpuError for GPUError { fn webgpu_error_type(&self) -> ErrorType { match self { GPUError::Lost => ErrorType::DeviceLost, GPUError::Validation(_) => ErrorType::Validation, GPUError::OutOfMemory => ErrorType::OutOfMemory, GPUError::Internal => ErrorType::Internal, } } } pub(crate) fn fmt_err(err: &(dyn std::error::Error + 'static)) -> String { let mut output = err.to_string(); let mut e = err.source(); while let Some(source) = e { output.push_str(&format!(": {source}")); e = source.source(); } if output.is_empty() { output.push_str("validation error"); } output } impl From for GPUError { fn from(err: EncoderStateError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: PassStateError) -> Self { GPUError::Validation(fmt_err(&err)) } } impl From for GPUError { fn from(err: CreateBufferError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: DeviceError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: BufferAccessError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateBindGroupLayoutError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreatePipelineLayoutError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateBindGroupError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: RenderBundleError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateRenderBundleError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CommandEncoderError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: QueryError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: ComputePassError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreatePipelineError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: GetBindGroupLayoutError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: RenderPassError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateSamplerError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateShaderModuleError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateTextureError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateTextureViewError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: CreateQuerySetError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: QueueSubmitError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: QueueWriteError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: ClearError) -> Self { GPUError::from_webgpu(err) } } impl From for GPUError { fn from(err: ConfigureSurfaceError) -> Self { GPUError::from_webgpu(err) } } #[derive(Debug, thiserror::Error, deno_error::JsError)] pub enum GPUGenericError { #[class(type)] #[error("Illegal constructor")] InvalidConstructor, } pub enum GPUPipelineErrorReason { Validation, #[expect(dead_code)] Internal, } impl Display for GPUPipelineErrorReason { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { Self::Validation => f.write_str("validation"), Self::Internal => f.write_str("internal"), } } } pub(crate) fn make_pipeline_error<'a>( scope: &mut v8::HandleScope<'a>, reason: GPUPipelineErrorReason, message: &str, ) -> v8::Local<'a, v8::Object> { let state = JsRuntime::op_state_from(scope); let class = state .borrow() .borrow::() .0 .clone(); let constructor = v8::Local::::try_from(v8::Local::new(scope, class)).unwrap(); let message_str = v8::String::new(scope, message).unwrap(); let reason_str = v8::String::new(scope, &reason.to_string()).unwrap(); let options = v8::Object::new(scope); let key = v8::String::new(scope, "reason").unwrap(); options.set(scope, key.into(), reason_str.into()); constructor .new_instance(scope, &[message_str.into(), options.into()]) .unwrap() }