Skip to main content

cotis_wgpu/
error.rs

1//! Initialization and presentation errors for the wgpu backend.
2//!
3//! Errors from [`WgpuBackendError`] fall into two categories:
4//!
5//! - **Startup failures** returned by [`crate::renderer::CotisWgpuRenderer::new`] and
6//!   [`crate::renderer::CotisWgpuRenderer::new_with_config`] — no adapter, surface creation,
7//!   event loop, or device request failures.
8//! - **Per-frame surface errors** returned internally during swapchain acquisition; these are
9//!   typically logged and the frame is skipped rather than propagated to application code.
10
11use std::error::Error;
12use std::fmt::{Display, Formatter};
13
14/// Errors that can occur while initializing or presenting through wgpu.
15///
16/// # Examples
17///
18/// ```
19/// use cotis_wgpu::WgpuBackendError;
20///
21/// assert_eq!(
22///     WgpuBackendError::NoAdapter.to_string(),
23///     "no compatible wgpu adapter found"
24/// );
25/// assert_eq!(
26///     WgpuBackendError::Init("window closed".into()).to_string(),
27///     "failed to initialize renderer: window closed"
28/// );
29/// ```
30#[derive(Debug)]
31pub enum WgpuBackendError {
32    /// No compatible GPU adapter was found during GPU device initialization.
33    ///
34    /// Occurs when wgpu cannot enumerate a suitable adapter for the current platform.
35    NoAdapter,
36    /// Failed to create the window surface from the winit window handle.
37    ///
38    /// Returned by [`crate::surface::SurfaceState::new`] when `create_surface_unsafe` fails.
39    CreateSurface(String),
40    /// Failed to initialize the winit event loop or create the window.
41    ///
42    /// Also returned when the window is closed before bootstrap completes.
43    Init(String),
44    /// Failed to request a logical device and queue from the adapter.
45    ///
46    /// Occurs when wgpu `request_device` fails during startup.
47    RequestDevice(String),
48    /// The swapchain surface reported an error while acquiring or presenting a frame.
49    ///
50    /// [`crate::surface::SurfaceState::acquire_frame`] retries once on
51    /// [`wgpu::SurfaceError::Lost`] or [`wgpu::SurfaceError::Outdated`] after reconfiguring.
52    Surface(wgpu::SurfaceError),
53}
54
55impl Display for WgpuBackendError {
56    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
57        match self {
58            Self::NoAdapter => write!(f, "no compatible wgpu adapter found"),
59            Self::CreateSurface(message) => write!(f, "failed to create surface: {message}"),
60            Self::Init(message) => write!(f, "failed to initialize renderer: {message}"),
61            Self::RequestDevice(message) => write!(f, "failed to request device: {message}"),
62            Self::Surface(error) => write!(f, "surface error: {error}"),
63        }
64    }
65}
66
67impl Error for WgpuBackendError {}