Skip to main content

beamterm_renderer/
error.rs

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