1use std::fmt;
2
3#[derive(Debug)]
4pub enum RenderError {
5 DeviceLost(String),
6 Surface(String),
7 MaterialCompile { name: String, reason: String },
8 ShaderValidation(String),
9 UnsupportedFormat(wgpu::TextureFormat),
10 VertexOverflow { needed: usize, max: usize },
11 FrameAcquire(String),
12}
13
14impl fmt::Display for RenderError {
15 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16 match self {
17 RenderError::DeviceLost(msg) => {
18 write!(f, "GPU device lost: {msg}. Try recreating the renderer.")
19 }
20 RenderError::Surface(msg) => {
21 write!(f, "Surface error: {msg}. Check window state and GPU availability.")
22 }
23 RenderError::MaterialCompile { name, reason } => {
24 write!(
25 f,
26 "Material compile failed for graph '{name}': {reason}. Validate node connections and types."
27 )
28 }
29 RenderError::ShaderValidation(msg) => {
30 write!(f, "Shader validation failed: {msg}. See inner WGSL error for line/column info.")
31 }
32 RenderError::UnsupportedFormat(fmt) => {
33 write!(
34 f,
35 "Surface format {fmt:?} not supported by this adapter. Try a different backend."
36 )
37 }
38 RenderError::VertexOverflow { needed, max } => {
39 write!(
40 f,
41 "Vertex buffer overflow: needed {needed} vertices, max is {max}. Batch geometry or increase pool size."
42 )
43 }
44 RenderError::FrameAcquire(msg) => {
45 write!(f, "Failed to acquire next frame from surface: {msg}")
46 }
47 }
48 }
49}
50
51impl std::error::Error for RenderError {}
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn device_lost_includes_message_and_suggestion() {
59 let err = RenderError::DeviceLost("GPU removed".into());
60 let msg = err.to_string();
61 assert!(msg.contains("GPU removed"), "should contain message");
62 assert!(msg.contains("recreating"), "should suggest recreating");
63 }
64
65 #[test]
66 fn material_compile_includes_name_and_reason() {
67 let err = RenderError::MaterialCompile {
68 name: "graph_0".into(),
69 reason: "cycle detected".into(),
70 };
71 let msg = err.to_string();
72 assert!(msg.contains("graph_0"), "should contain graph name");
73 assert!(msg.contains("cycle detected"), "should contain reason");
74 assert!(msg.contains("node connections"), "should suggest fix");
75 }
76
77 #[test]
78 fn vertex_overflow_includes_counts() {
79 let err = RenderError::VertexOverflow { needed: 100000, max: 65536 };
80 let msg = err.to_string();
81 assert!(msg.contains("100000"), "should contain needed count");
82 assert!(msg.contains("65536"), "should contain max count");
83 }
84
85 #[test]
86 fn shader_validation_includes_detail() {
87 let err = RenderError::ShaderValidation("type mismatch at line 42".into());
88 let msg = err.to_string();
89 assert!(msg.contains("type mismatch"), "should contain detail");
90 assert!(msg.contains("WGSL"), "should mention WGSL");
91 }
92
93 #[test]
94 fn error_trait_satisfied() {
95 let _boxed: Box<dyn std::error::Error> = Box::new(RenderError::FrameAcquire("test".into()));
96 }
97}