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
25impl Error {
26    // Helper constructors for common error scenarios
27
28    // Initialization errors
29    pub fn window_not_found() -> Self {
30        Self::Initialization("Unable to retrieve window".to_string())
31    }
32
33    pub fn document_not_found() -> Self {
34        Self::Initialization("Unable to retrieve document".to_string())
35    }
36
37    pub fn canvas_not_found() -> Self {
38        Self::Initialization("Unable to retrieve canvas".to_string())
39    }
40
41    pub fn webgl_context_failed() -> Self {
42        Self::Initialization("Failed to retrieve WebGL2 rendering context".to_string())
43    }
44
45    pub fn canvas_context_failed() -> Self {
46        Self::Initialization("Failed to retrieve canvas rendering context".to_string())
47    }
48
49    // Shader errors
50    pub fn shader_creation_failed(detail: &str) -> Self {
51        Self::Shader(format!("Shader creation failed: {detail}"))
52    }
53
54    pub fn shader_program_creation_failed() -> Self {
55        Self::Shader("Shader program creation failed".to_string())
56    }
57
58    pub fn shader_link_failed(log: String) -> Self {
59        Self::Shader(format!("Shader linking failed: {log}"))
60    }
61
62    // Resource errors
63    pub fn buffer_creation_failed(buffer_type: &str) -> Self {
64        Self::Resource(format!("Failed to create {buffer_type} buffer"))
65    }
66
67    pub fn vertex_array_creation_failed() -> Self {
68        Self::Resource("Failed to create vertex array object".to_string())
69    }
70
71    pub fn texture_creation_failed() -> Self {
72        Self::Resource("Failed to create texture".to_string())
73    }
74
75    pub fn uniform_location_failed(name: &str) -> Self {
76        Self::Resource(format!("Failed to get uniform location: {name}"))
77    }
78
79    pub fn webgl_error(message: String) -> Self {
80        Self::Resource(format!("WebGL error: {message}"))
81    }
82
83    pub fn element_creation_failed(element_type: &str) -> Self {
84        Self::Resource(format!("Failed to create element: {element_type}"))
85    }
86
87    // Data errors
88    pub fn image_load_failed(path: &str) -> Self {
89        Self::Data(format!("Failed to load image: {path}"))
90    }
91
92    pub fn deserialization_failed(message: String) -> Self {
93        Self::Data(format!("Failed to deserialize: {message}"))
94    }
95}