goud_engine/core/error/codes.rs
1//! FFI-compatible error code constants organized by category.
2//!
3//! Error codes use `i32` for maximum C ABI compatibility. Negative values
4//! are reserved for future use (e.g., platform-specific errors).
5//!
6//! # Error Code Ranges
7//!
8//! | Range | Category | Description |
9//! |-----------|------------|--------------------------------------|
10//! | 0 | Success | Operation completed successfully |
11//! | 1-99 | Context | Initialization and context errors |
12//! | 100-199 | Resource | Asset and resource management errors |
13//! | 200-299 | Graphics | Rendering and GPU errors |
14//! | 300-399 | Entity | ECS entity and component errors |
15//! | 400-499 | Input | Input handling errors |
16//! | 500-599 | System | Platform and system errors |
17//! | 600-699 | Provider | Provider subsystem errors |
18//! | 900-999 | Internal | Unexpected internal errors |
19
20/// FFI-compatible error code type.
21///
22/// Uses `i32` for C ABI compatibility across all platforms.
23/// Negative values are reserved for future platform-specific errors.
24pub type GoudErrorCode = i32;
25
26// =============================================================================
27// Error Code Constants
28// =============================================================================
29
30/// Operation completed successfully.
31pub const SUCCESS: GoudErrorCode = 0;
32
33// -----------------------------------------------------------------------------
34// Context Errors (1-99): Engine initialization and context management
35// -----------------------------------------------------------------------------
36
37/// Base code for context/initialization errors.
38pub const CONTEXT_ERROR_BASE: GoudErrorCode = 1;
39
40/// Engine has not been initialized.
41/// Recovery: call the initialization function first.
42pub const ERR_NOT_INITIALIZED: GoudErrorCode = 1;
43
44/// Engine has already been initialized.
45/// Recovery: shut down the engine before re-initializing.
46pub const ERR_ALREADY_INITIALIZED: GoudErrorCode = 2;
47
48/// Invalid engine context.
49/// Recovery: ensure the context was properly created and not corrupted.
50pub const ERR_INVALID_CONTEXT: GoudErrorCode = 3;
51
52/// Engine context has been destroyed.
53/// Recovery: re-initialize the engine to obtain a new context.
54pub const ERR_CONTEXT_DESTROYED: GoudErrorCode = 4;
55
56/// Engine initialization failed (generic).
57/// Specific error message available via `last_error_message()`.
58/// Recovery: check the error message for details and verify dependencies.
59pub const ERR_INITIALIZATION_FAILED: GoudErrorCode = 10;
60
61// -----------------------------------------------------------------------------
62// Resource Errors (100-199): Asset loading and resource management
63// -----------------------------------------------------------------------------
64
65/// Base code for resource/asset errors.
66pub const RESOURCE_ERROR_BASE: GoudErrorCode = 100;
67
68/// Requested resource was not found.
69/// Recovery: verify the file path and check the working directory.
70pub const ERR_RESOURCE_NOT_FOUND: GoudErrorCode = 100;
71
72/// Failed to load resource from source.
73/// Recovery: check file permissions and ensure the file is not locked.
74pub const ERR_RESOURCE_LOAD_FAILED: GoudErrorCode = 101;
75
76/// Resource format is invalid or unsupported.
77/// Recovery: verify the file is not corrupted and uses a supported format.
78pub const ERR_RESOURCE_INVALID_FORMAT: GoudErrorCode = 102;
79
80/// Resource with this identifier already exists.
81/// Recovery: use a unique identifier or remove the existing resource first.
82pub const ERR_RESOURCE_ALREADY_EXISTS: GoudErrorCode = 103;
83
84/// Handle is invalid (null or malformed).
85/// Recovery: ensure the handle was obtained from a valid creation call.
86pub const ERR_INVALID_HANDLE: GoudErrorCode = 110;
87
88/// Handle refers to a resource that has been deallocated.
89/// Recovery: re-create the resource to get a new handle.
90pub const ERR_HANDLE_EXPIRED: GoudErrorCode = 111;
91
92/// Handle type does not match expected resource type.
93/// Recovery: pass the correct handle type for the operation.
94pub const ERR_HANDLE_TYPE_MISMATCH: GoudErrorCode = 112;
95
96// -----------------------------------------------------------------------------
97// Graphics Errors (200-299): Rendering and GPU operations
98// -----------------------------------------------------------------------------
99
100/// Base code for graphics/rendering errors.
101pub const GRAPHICS_ERROR_BASE: GoudErrorCode = 200;
102
103/// Shader compilation failed.
104/// Recovery: review shader source; the error message contains GPU compiler output.
105pub const ERR_SHADER_COMPILATION_FAILED: GoudErrorCode = 200;
106
107/// Shader program linking failed.
108/// Recovery: verify shader stage inputs/outputs match and uniforms are declared.
109pub const ERR_SHADER_LINK_FAILED: GoudErrorCode = 201;
110
111/// Texture creation failed.
112/// Recovery: check texture dimensions and format; reduce size or free GPU resources.
113pub const ERR_TEXTURE_CREATION_FAILED: GoudErrorCode = 210;
114
115/// Buffer creation failed.
116/// Recovery: reduce buffer size or free unused GPU buffers.
117pub const ERR_BUFFER_CREATION_FAILED: GoudErrorCode = 211;
118
119/// Render target creation failed.
120/// Recovery: verify attachment formats and dimensions are consistent.
121pub const ERR_RENDER_TARGET_FAILED: GoudErrorCode = 220;
122
123/// Graphics backend not supported on this platform.
124/// Recovery: update GPU drivers or select a different supported backend.
125pub const ERR_BACKEND_NOT_SUPPORTED: GoudErrorCode = 230;
126
127/// Draw call failed.
128/// Recovery: verify buffer bindings and shader state; try updating GPU drivers.
129pub const ERR_DRAW_CALL_FAILED: GoudErrorCode = 240;
130
131// -----------------------------------------------------------------------------
132// Entity Errors (300-399): ECS entity and component operations
133// -----------------------------------------------------------------------------
134
135/// Base code for ECS entity errors.
136pub const ENTITY_ERROR_BASE: GoudErrorCode = 300;
137
138/// Entity was not found.
139/// Recovery: verify the entity ID is valid and has not been despawned.
140pub const ERR_ENTITY_NOT_FOUND: GoudErrorCode = 300;
141
142/// Entity already exists.
143/// Recovery: use a different entity ID or remove the existing entity first.
144pub const ERR_ENTITY_ALREADY_EXISTS: GoudErrorCode = 301;
145
146/// Component was not found on entity.
147/// Recovery: attach the component before accessing it, or check with a has-component query.
148pub const ERR_COMPONENT_NOT_FOUND: GoudErrorCode = 310;
149
150/// Component already exists on entity.
151/// Recovery: use replace/update instead of add, or remove the existing component first.
152pub const ERR_COMPONENT_ALREADY_EXISTS: GoudErrorCode = 311;
153
154/// Query execution failed.
155/// Recovery: check for conflicting mutable/immutable access on the same component.
156pub const ERR_QUERY_FAILED: GoudErrorCode = 320;
157
158// -----------------------------------------------------------------------------
159// Input Errors (400-499): Input handling
160// -----------------------------------------------------------------------------
161
162/// Base code for input handling errors.
163pub const INPUT_ERROR_BASE: GoudErrorCode = 400;
164
165/// Input device not found or disconnected.
166/// Recovery: verify the input device is connected and recognized by the OS.
167pub const ERR_INPUT_DEVICE_NOT_FOUND: GoudErrorCode = 400;
168
169/// Invalid input action name.
170/// Recovery: check the action name matches a registered input action.
171pub const ERR_INVALID_INPUT_ACTION: GoudErrorCode = 401;
172
173// -----------------------------------------------------------------------------
174// System Errors (500-599): Platform and system operations
175// -----------------------------------------------------------------------------
176
177/// Base code for system/platform errors.
178pub const SYSTEM_ERROR_BASE: GoudErrorCode = 500;
179
180/// Window creation failed.
181/// Recovery: verify display server is running and window parameters are valid.
182pub const ERR_WINDOW_CREATION_FAILED: GoudErrorCode = 500;
183
184/// Audio system initialization failed.
185/// Recovery: check that an audio output device is available.
186pub const ERR_AUDIO_INIT_FAILED: GoudErrorCode = 510;
187
188/// Physics system initialization failed.
189/// Recovery: review physics configuration for invalid values.
190pub const ERR_PHYSICS_INIT_FAILED: GoudErrorCode = 520;
191
192/// Generic platform error.
193/// Recovery: check the error message for platform-specific details.
194pub const ERR_PLATFORM_ERROR: GoudErrorCode = 530;
195
196// -----------------------------------------------------------------------------
197// Provider Errors (600-699): Provider subsystem errors
198// -----------------------------------------------------------------------------
199
200/// Base code for provider errors.
201pub const PROVIDER_ERROR_BASE: GoudErrorCode = 600;
202
203/// Provider initialization failed.
204pub const ERR_PROVIDER_INIT_FAILED: GoudErrorCode = 600;
205
206/// Provider was not found or not registered.
207pub const ERR_PROVIDER_NOT_FOUND: GoudErrorCode = 601;
208
209/// Provider operation failed.
210pub const ERR_PROVIDER_OPERATION_FAILED: GoudErrorCode = 602;
211
212// Provider subsystem ranges:
213// 600-609: Render provider errors
214// 610-619: Physics provider errors
215// 620-629: Audio provider errors
216// 630-639: Window provider errors
217// 640-649: Input provider errors
218// 700-709: Reserved for future use (network provider uses generic ProviderError)
219
220// -----------------------------------------------------------------------------
221// Internal Errors (900-999): Unexpected internal errors
222// -----------------------------------------------------------------------------
223
224/// Base code for internal/unexpected errors.
225pub const INTERNAL_ERROR_BASE: GoudErrorCode = 900;
226
227/// Internal engine error (unexpected state).
228/// Recovery: report the error with full details; this is likely an engine bug.
229pub const ERR_INTERNAL_ERROR: GoudErrorCode = 900;
230
231/// Feature not yet implemented.
232/// Recovery: use an alternative approach or wait for the feature to be implemented.
233pub const ERR_NOT_IMPLEMENTED: GoudErrorCode = 901;
234
235/// Invalid engine state.
236/// Recovery: check the sequence of API calls; the engine may need re-initialization.
237pub const ERR_INVALID_STATE: GoudErrorCode = 902;
238
239// =============================================================================
240// Helper Functions
241// =============================================================================
242
243/// Returns the category name for an error code.
244///
245/// # Examples
246///
247/// ```
248/// use goud_engine::core::error::{error_category, SUCCESS, ERR_RESOURCE_NOT_FOUND};
249///
250/// assert_eq!(error_category(SUCCESS), "Success");
251/// assert_eq!(error_category(ERR_RESOURCE_NOT_FOUND), "Resource");
252/// ```
253#[inline]
254pub const fn error_category(code: GoudErrorCode) -> &'static str {
255 match code {
256 SUCCESS => "Success",
257 1..=99 => "Context",
258 100..=199 => "Resource",
259 200..=299 => "Graphics",
260 300..=399 => "Entity",
261 400..=499 => "Input",
262 500..=599 => "System",
263 600..=699 => "Provider",
264 900..=999 => "Internal",
265 _ => "Unknown",
266 }
267}
268
269/// Returns true if the error code indicates success.
270#[inline]
271pub const fn is_success(code: GoudErrorCode) -> bool {
272 code == SUCCESS
273}
274
275/// Returns true if the error code indicates an error.
276#[inline]
277pub const fn is_error(code: GoudErrorCode) -> bool {
278 code != SUCCESS
279}