Skip to main content

beamterm_renderer/
error.rs

1/// Error categories.
2#[derive(thiserror::Error, Debug)]
3#[non_exhaustive]
4pub enum Error {
5    /// Failed to initialize WebGL context or retrieve DOM elements.
6    #[error("Initialization error: {0}")]
7    Initialization(String),
8
9    /// Shader compilation, linking, or program creation errors.
10    #[error("Shader error: {0}")]
11    Shader(String),
12
13    /// WebGL resource creation or management errors.
14    #[error("Resource error: {0}")]
15    Resource(String),
16
17    /// External data loading or parsing errors.
18    #[error("Data error: {0}")]
19    Data(String),
20
21    /// Event listener errors, related to mouse input handling.
22    #[error("Event listener error: {0}")]
23    Callback(String),
24
25    /// Canvas rasterization errors during dynamic font atlas generation.
26    #[error("Rasterization error: {0}")]
27    Rasterization(String),
28}
29
30impl Error {
31    // Helper constructors for common error scenarios
32
33    // Initialization errors
34    pub(crate) fn window_not_found() -> Self {
35        Self::Initialization("Unable to retrieve window".to_string())
36    }
37
38    pub(crate) fn document_not_found() -> Self {
39        Self::Initialization("Unable to retrieve document".to_string())
40    }
41
42    pub(crate) fn canvas_not_found() -> Self {
43        Self::Initialization("Unable to retrieve canvas".to_string())
44    }
45
46    #[cfg(target_arch = "wasm32")]
47    pub(crate) fn webgl_context_failed() -> Self {
48        Self::Initialization("Failed to retrieve WebGL2 rendering context".to_string())
49    }
50
51    #[cfg(target_arch = "wasm32")]
52    pub(crate) fn canvas_context_failed() -> Self {
53        Self::Initialization("Failed to retrieve canvas rendering context".to_string())
54    }
55
56    pub(crate) fn rasterizer_canvas_creation_failed(detail: impl std::fmt::Display) -> Self {
57        Self::Rasterization(format!("Failed to create offscreen canvas: {detail}"))
58    }
59
60    pub(crate) fn rasterizer_context_failed() -> Self {
61        Self::Rasterization("Failed to get 2d rendering context from offscreen canvas".to_string())
62    }
63
64    pub(crate) fn rasterizer_fill_text_failed(
65        grapheme: &str,
66        detail: impl std::fmt::Display,
67    ) -> Self {
68        Self::Rasterization(format!("Failed to render glyph '{grapheme}': {detail}"))
69    }
70
71    pub(crate) fn rasterizer_get_image_data_failed(detail: impl std::fmt::Display) -> Self {
72        Self::Rasterization(format!("Failed to read pixel data from canvas: {detail}"))
73    }
74
75    pub(crate) fn rasterizer_measure_failed(detail: impl std::fmt::Display) -> Self {
76        Self::Rasterization(format!("Failed to measure cell metrics: {detail}"))
77    }
78
79    pub(crate) fn rasterizer_empty_reference_glyph() -> Self {
80        Self::Rasterization(
81            "Reference glyph rasterization produced no pixels; cannot determine cell size"
82                .to_string(),
83        )
84    }
85}
86
87impl From<beamterm_core::Error> for Error {
88    fn from(err: beamterm_core::Error) -> Self {
89        match err {
90            beamterm_core::Error::Shader(msg) => Error::Shader(msg),
91            beamterm_core::Error::Resource(msg) => Error::Resource(msg),
92            beamterm_core::Error::Data(msg) => Error::Data(msg),
93            other => Error::Resource(other.to_string()),
94        }
95    }
96}
97
98impl From<Error> for wasm_bindgen::JsValue {
99    fn from(err: Error) -> Self {
100        wasm_bindgen::JsValue::from_str(&err.to_string())
101    }
102}