Skip to main content

retroglyph_gl/
error.rs

1//! [`SurfaceError`]: the GL backend's surface-lifecycle error type.
2
3use std::fmt;
4
5/// A failure creating or driving the GL context/surface.
6///
7/// Both the native (glutin) and wasm (WebGL2) context modules produce this. It is string-backed
8/// rather than a structured enum because the two platforms surface very different underlying error
9/// types (glutin's `glutin::error::Error` vs. a `web_sys` `JsValue`), and the caller
10/// ([`retroglyph_window`]'s event loop) only needs a message plus the recoverable/fatal signal
11/// from [`RecoverableError`](retroglyph_window::RecoverableError).
12#[derive(Debug)]
13#[non_exhaustive]
14pub enum SurfaceError {
15    /// Creating the GL display, config, context, or surface failed. Treated as fatal (not
16    /// recoverable): a game cannot proceed without a context, and retrying the same creation
17    /// path is very unlikely to succeed.
18    Init(String),
19    /// Presenting a frame failed (buffer swap on native, or the WebGL2 context was lost on wasm).
20    /// Treated as potentially recoverable so the event loop's consecutive-failure heuristic can
21    /// retry before giving up.
22    Present(String),
23}
24
25impl fmt::Display for SurfaceError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Self::Init(msg) => write!(f, "GL surface init: {msg}"),
29            Self::Present(msg) => write!(f, "GL surface present: {msg}"),
30        }
31    }
32}
33
34impl std::error::Error for SurfaceError {}
35
36impl retroglyph_window::RecoverableError for SurfaceError {
37    fn is_recoverable(&self) -> bool {
38        // Init failures are fatal (nothing to retry into); present failures may be transient
39        // (e.g. a wasm context-loss that the browser later restores).
40        matches!(self, Self::Present(_))
41    }
42}