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    pub(crate) fn webgl_context_failed() -> Self {
46        Self::Initialization("Failed to retrieve WebGL2 rendering context".to_string())
47    }
48
49    pub(crate) fn canvas_context_failed() -> Self {
50        Self::Initialization("Failed to retrieve canvas rendering context".to_string())
51    }
52
53    // Shader errors
54    pub(crate) fn shader_creation_failed(detail: &str) -> Self {
55        Self::Shader(format!("Shader creation failed: {detail}"))
56    }
57
58    pub(crate) fn shader_program_creation_failed() -> Self {
59        Self::Shader("Shader program creation failed".to_string())
60    }
61
62    pub(crate) fn shader_link_failed(log: String) -> Self {
63        Self::Shader(format!("Shader linking failed: {log}"))
64    }
65
66    // Resource errors
67    pub(crate) fn buffer_creation_failed(buffer_type: &str) -> Self {
68        Self::Resource(format!("Failed to create {buffer_type} buffer"))
69    }
70
71    pub(crate) fn vertex_array_creation_failed() -> Self {
72        Self::Resource("Failed to create vertex array object".to_string())
73    }
74
75    pub(crate) fn texture_creation_failed() -> Self {
76        Self::Resource("Failed to create texture".to_string())
77    }
78
79    pub(crate) fn rasterizer_canvas_creation_failed(detail: impl std::fmt::Display) -> Self {
80        Self::Rasterization(format!("Failed to create offscreen canvas: {detail}"))
81    }
82
83    pub(crate) fn rasterizer_context_failed() -> Self {
84        Self::Rasterization("Failed to get 2d rendering context from offscreen canvas".to_string())
85    }
86
87    pub(crate) fn rasterizer_missing_font_family() -> Self {
88        Self::Rasterization("font_family must be set before rasterizing".to_string())
89    }
90
91    pub(crate) fn rasterizer_missing_font_size() -> Self {
92        Self::Rasterization("font_size must be set before rasterizing".to_string())
93    }
94
95    pub(crate) fn rasterizer_fill_text_failed(
96        grapheme: &str,
97        detail: impl std::fmt::Display,
98    ) -> Self {
99        Self::Rasterization(format!("Failed to render glyph '{grapheme}': {detail}"))
100    }
101
102    pub(crate) fn rasterizer_get_image_data_failed(detail: impl std::fmt::Display) -> Self {
103        Self::Rasterization(format!("Failed to read pixel data from canvas: {detail}"))
104    }
105
106    pub(crate) fn rasterizer_measure_failed(detail: impl std::fmt::Display) -> Self {
107        Self::Rasterization(format!("Failed to measure cell metrics: {detail}"))
108    }
109
110    pub(crate) fn rasterizer_empty_reference_glyph() -> Self {
111        Self::Rasterization(
112            "Reference glyph rasterization produced no pixels; cannot determine cell size"
113                .to_string(),
114        )
115    }
116
117    pub(crate) fn uniform_location_failed(name: &str) -> Self {
118        Self::Resource(format!("Failed to get uniform location: {name}"))
119    }
120}
121
122impl From<Error> for wasm_bindgen::JsValue {
123    fn from(err: Error) -> Self {
124        wasm_bindgen::JsValue::from_str(&err.to_string())
125    }
126}