myth_core/errors.rs
1//! Error types for the Myth engine.
2//!
3//! # Hierarchy
4//!
5//! - [`enum@Error`] — top-level enum delegating to sub-categories
6//! - [`PlatformError`] — window system and adapter errors
7//! - [`AssetError`] — I/O, network, parsing errors
8//! - [`RenderError`] — GPU device and shader errors
9
10use thiserror::Error;
11
12// ============================================================================
13// Top-Level Error
14// ============================================================================
15
16/// Top-level error type for the Myth engine.
17#[derive(Error, Debug)]
18pub enum Error {
19 /// Platform and window system errors.
20 #[error("Platform error: {0}")]
21 Platform(#[from] PlatformError),
22
23 /// Asset loading and processing errors.
24 #[error("Asset error: {0}")]
25 Asset(#[from] AssetError),
26
27 /// Rendering and GPU errors.
28 #[error("Render error: {0}")]
29 Render(#[from] RenderError),
30
31 /// General engine error.
32 #[error("Engine error: {0}")]
33 General(String),
34}
35
36// ============================================================================
37// Asset Errors
38// ============================================================================
39
40/// Errors related to asset loading and processing.
41#[derive(Error, Debug)]
42pub enum AssetError {
43 /// Asset not found.
44 #[error("Asset not found: {0}")]
45 NotFound(String),
46
47 /// File I/O error.
48 #[error("Failed to load file: {0}")]
49 Io(#[from] std::io::Error),
50
51 /// HTTP request failed.
52 #[error("Network request failed: {0}")]
53 Network(String),
54
55 /// HTTP response with non-success status.
56 #[error("HTTP response error: status {status}")]
57 HttpResponse {
58 /// HTTP status code.
59 status: u16,
60 },
61
62 /// Data format parsing failure.
63 #[error("Failed to parse data format: {0}")]
64 Format(String),
65
66 /// Invalid asset data.
67 #[error("Invalid asset data: {0}")]
68 InvalidData(String),
69
70 /// Base64 decoding error.
71 #[error("Base64 decode error: {0}")]
72 Base64Decode(String),
73
74 /// Async task join error.
75 #[error("Task join error: {0}")]
76 TaskJoin(String),
77}
78
79// ============================================================================
80// Platform Errors
81// ============================================================================
82
83/// Errors related to platform and window system.
84#[derive(Error, Debug)]
85pub enum PlatformError {
86 /// Window handle error.
87 #[error("Window handle error: {0}")]
88 WindowHandle(String),
89
90 /// Event loop error.
91 #[error("Event loop error: {0}")]
92 EventLoop(String),
93
94 /// No compatible GPU adapter found.
95 #[error("No compatible GPU adapter found: {0}")]
96 AdapterNotFound(String),
97
98 /// Surface configuration failed.
99 #[error("Failed to create surface: {0}")]
100 SurfaceConfigFailed(String),
101
102 /// WASM-specific error.
103 #[error("WASM error: {0}")]
104 Wasm(String),
105
106 /// Feature not enabled.
107 #[error("Feature not enabled: {0}")]
108 FeatureNotEnabled(String),
109}
110
111// ============================================================================
112// Render Errors
113// ============================================================================
114
115/// Errors related to rendering and GPU operations.
116#[derive(Error, Debug)]
117pub enum RenderError {
118 /// GPU device creation error.
119 #[error("WGPU device error: {0}")]
120 RequestDeviceFailed(String),
121
122 /// Shader compilation failed.
123 #[error("Shader compilation failed: {0}")]
124 ShaderCompile(String),
125
126 /// Render graph error.
127 #[error("Render graph error: {0}")]
128 Graph(String),
129}
130
131// ============================================================================
132// Convenient conversion: std::io::Error → Error (via AssetError)
133// ============================================================================
134
135impl From<std::io::Error> for Error {
136 fn from(err: std::io::Error) -> Self {
137 Error::Asset(AssetError::from(err))
138 }
139}
140
141/// Alias for `Result<T, Error>`.
142pub type Result<T> = std::result::Result<T, Error>;