Skip to main content

goud_engine/core/error/
methods.rs

1//! Implementation methods for `GoudError`.
2
3use super::codes::*;
4use super::types::GoudError;
5
6impl GoudError {
7    /// Returns the FFI-compatible error code for this error.
8    ///
9    /// This method maps each error variant to its corresponding error code constant,
10    /// which can be safely passed across the FFI boundary to C#, Python, or other
11    /// language bindings.
12    ///
13    /// # Example
14    ///
15    /// ```
16    /// use goud_engine::core::error::{GoudError, ERR_NOT_INITIALIZED, ERR_INITIALIZATION_FAILED};
17    ///
18    /// assert_eq!(GoudError::NotInitialized.error_code(), ERR_NOT_INITIALIZED);
19    /// assert_eq!(
20    ///     GoudError::InitializationFailed("GPU not found".to_string()).error_code(),
21    ///     ERR_INITIALIZATION_FAILED
22    /// );
23    /// ```
24    #[inline]
25    pub const fn error_code(&self) -> GoudErrorCode {
26        match self {
27            // Context errors (1-99)
28            GoudError::NotInitialized => ERR_NOT_INITIALIZED,
29            GoudError::AlreadyInitialized => ERR_ALREADY_INITIALIZED,
30            GoudError::InvalidContext => ERR_INVALID_CONTEXT,
31            GoudError::ContextDestroyed => ERR_CONTEXT_DESTROYED,
32            GoudError::InitializationFailed(_) => ERR_INITIALIZATION_FAILED,
33
34            // Resource errors (100-199)
35            GoudError::ResourceNotFound(_) => ERR_RESOURCE_NOT_FOUND,
36            GoudError::ResourceLoadFailed(_) => ERR_RESOURCE_LOAD_FAILED,
37            GoudError::ResourceInvalidFormat(_) => ERR_RESOURCE_INVALID_FORMAT,
38            GoudError::ResourceAlreadyExists(_) => ERR_RESOURCE_ALREADY_EXISTS,
39            GoudError::InvalidHandle => ERR_INVALID_HANDLE,
40            GoudError::HandleExpired => ERR_HANDLE_EXPIRED,
41            GoudError::HandleTypeMismatch => ERR_HANDLE_TYPE_MISMATCH,
42
43            // Graphics errors (200-299)
44            GoudError::ShaderCompilationFailed(_) => ERR_SHADER_COMPILATION_FAILED,
45            GoudError::ShaderLinkFailed(_) => ERR_SHADER_LINK_FAILED,
46            GoudError::TextureCreationFailed(_) => ERR_TEXTURE_CREATION_FAILED,
47            GoudError::BufferCreationFailed(_) => ERR_BUFFER_CREATION_FAILED,
48            GoudError::RenderTargetFailed(_) => ERR_RENDER_TARGET_FAILED,
49            GoudError::BackendNotSupported(_) => ERR_BACKEND_NOT_SUPPORTED,
50            GoudError::DrawCallFailed(_) => ERR_DRAW_CALL_FAILED,
51
52            // Entity errors (300-399)
53            GoudError::EntityNotFound => ERR_ENTITY_NOT_FOUND,
54            GoudError::EntityAlreadyExists => ERR_ENTITY_ALREADY_EXISTS,
55            GoudError::ComponentNotFound => ERR_COMPONENT_NOT_FOUND,
56            GoudError::ComponentAlreadyExists => ERR_COMPONENT_ALREADY_EXISTS,
57            GoudError::QueryFailed(_) => ERR_QUERY_FAILED,
58
59            // Input errors (400-499)
60            GoudError::InputDeviceNotFound => ERR_INPUT_DEVICE_NOT_FOUND,
61            GoudError::InvalidInputAction(_) => ERR_INVALID_INPUT_ACTION,
62
63            // System errors (500-599)
64            GoudError::WindowCreationFailed(_) => ERR_WINDOW_CREATION_FAILED,
65            GoudError::AudioInitFailed(_) => ERR_AUDIO_INIT_FAILED,
66            GoudError::PhysicsInitFailed(_) => ERR_PHYSICS_INIT_FAILED,
67            GoudError::PlatformError(_) => ERR_PLATFORM_ERROR,
68
69            // Provider errors (600-699)
70            GoudError::ProviderError { .. } => ERR_PROVIDER_OPERATION_FAILED,
71
72            // Internal errors (900-999)
73            GoudError::InternalError(_) => ERR_INTERNAL_ERROR,
74            GoudError::NotImplemented(_) => ERR_NOT_IMPLEMENTED,
75            GoudError::InvalidState(_) => ERR_INVALID_STATE,
76        }
77    }
78
79    /// Returns the error category as a static string.
80    ///
81    /// This is a convenience method that returns the category name for this error.
82    ///
83    /// # Example
84    ///
85    /// ```
86    /// use goud_engine::core::error::GoudError;
87    ///
88    /// assert_eq!(GoudError::NotInitialized.category(), "Context");
89    /// ```
90    #[inline]
91    pub const fn category(&self) -> &'static str {
92        error_category(self.error_code())
93    }
94
95    /// Returns the error message for errors that contain one, or a default description.
96    ///
97    /// For errors with associated string messages, this returns the message.
98    /// For errors without messages, this returns a default description.
99    ///
100    /// # Example
101    ///
102    /// ```
103    /// use goud_engine::core::error::GoudError;
104    ///
105    /// let error = GoudError::InitializationFailed("GPU not found".to_string());
106    /// assert_eq!(error.message(), "GPU not found");
107    ///
108    /// let error = GoudError::NotInitialized;
109    /// assert_eq!(error.message(), "Engine has not been initialized");
110    /// ```
111    pub fn message(&self) -> &str {
112        match self {
113            // Context errors
114            GoudError::NotInitialized => "Engine has not been initialized",
115            GoudError::AlreadyInitialized => "Engine has already been initialized",
116            GoudError::InvalidContext => "Invalid engine context",
117            GoudError::ContextDestroyed => "Engine context has been destroyed",
118            GoudError::InitializationFailed(msg) => msg,
119
120            // Resource errors
121            GoudError::ResourceNotFound(msg) => msg,
122            GoudError::ResourceLoadFailed(msg) => msg,
123            GoudError::ResourceInvalidFormat(msg) => msg,
124            GoudError::ResourceAlreadyExists(msg) => msg,
125            GoudError::InvalidHandle => "Invalid handle",
126            GoudError::HandleExpired => "Handle has expired",
127            GoudError::HandleTypeMismatch => "Handle type mismatch",
128
129            // Graphics errors
130            GoudError::ShaderCompilationFailed(msg) => msg,
131            GoudError::ShaderLinkFailed(msg) => msg,
132            GoudError::TextureCreationFailed(msg) => msg,
133            GoudError::BufferCreationFailed(msg) => msg,
134            GoudError::RenderTargetFailed(msg) => msg,
135            GoudError::BackendNotSupported(msg) => msg,
136            GoudError::DrawCallFailed(msg) => msg,
137
138            // Entity errors
139            GoudError::EntityNotFound => "Entity not found",
140            GoudError::EntityAlreadyExists => "Entity already exists",
141            GoudError::ComponentNotFound => "Component not found",
142            GoudError::ComponentAlreadyExists => "Component already exists",
143            GoudError::QueryFailed(msg) => msg,
144
145            // Input errors
146            GoudError::InputDeviceNotFound => "Input device not found",
147            GoudError::InvalidInputAction(msg) => msg,
148
149            // System errors
150            GoudError::WindowCreationFailed(msg) => msg,
151            GoudError::AudioInitFailed(msg) => msg,
152            GoudError::PhysicsInitFailed(msg) => msg,
153            GoudError::PlatformError(msg) => msg,
154
155            // Provider errors
156            GoudError::ProviderError { message, .. } => message,
157
158            // Internal errors
159            GoudError::InternalError(msg) => msg,
160            GoudError::NotImplemented(msg) => msg,
161            GoudError::InvalidState(msg) => msg,
162        }
163    }
164}