/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use std::{borrow::Cow, error::Error}; use serde::{Deserialize, Serialize}; use wgpu_core_remote_types::id; use wgt::error::{ErrorType, WebGpuError}; pub fn error_to_string(error: impl Error) -> String { use std::fmt::Write; let mut message = format!("{}", error); let mut e = error.source(); while let Some(source) = e { write!(message, ", caused by: {}", source).unwrap(); e = source.source(); } message } pub(crate) fn request_device_error_to_ffi( err: wgpu_core_remote_types::RequestDeviceError, ) -> crate::RequestDeviceError { use crate::FfiRequestDeviceErrorType as Ty; use wgpu_core_remote_types::RequestDeviceError as E; let (ty, message) = match err { E::UnsupportedFeature(message) => (Ty::TypeError, message), E::FailedLimit(message) => (Ty::OperationError, message), E::Other(message) => (Ty::OperationError, message), }; crate::RequestDeviceError { ty, message } } pub(crate) fn pipeline_error_to_ffi( err: wgpu_core_remote_types::PipelineError, ) -> crate::PipelineError { use wgpu_core_remote_types::PipelineError as E; let (is_validation_error, error) = match err { E::Validation(message) => (true, message), E::Internal(message) => (false, message), }; crate::PipelineError { is_validation_error, error, } } #[derive(Debug, thiserror::Error, Serialize, Deserialize)] #[error("{message}")] pub struct GPUError<'a> { pub message: Cow<'a, str>, pub r#type: ErrorType, } impl WebGpuError for GPUError<'static> { fn webgpu_error_type(&self) -> ErrorType { self.r#type } } impl GPUError<'static> { pub fn oom() -> Self { Self { message: "Out of memory".into(), r#type: ErrorType::OutOfMemory, } } pub fn report(self, global: &wgpu_core_remote::global::Global, device_id: id::DeviceId) { global.device_handle_error(device_id, self, None, ""); } }