elif_orm/
error.rs

1//! Error types for the ORM system
2//!
3//! Provides comprehensive error handling for database operations,
4//! model validation, and query building.
5
6use std::fmt;
7
8/// Result type alias for model operations
9pub type ModelResult<T> = Result<T, ModelError>;
10
11/// ORM error type alias
12pub type OrmError = ModelError;
13
14/// ORM result type alias  
15pub type OrmResult<T> = ModelResult<T>;
16
17/// Error types for ORM operations
18#[derive(Debug, Clone)]
19pub enum ModelError {
20    /// Database connection or query error
21    Database(String),
22    /// Model not found in database
23    NotFound(String),
24    /// Model validation failed
25    Validation(String),
26    /// Primary key is missing or invalid
27    MissingPrimaryKey,
28    /// Relationship loading failed
29    Relationship(String),
30    /// Serialization/deserialization error
31    Serialization(String),
32    /// Migration error
33    Migration(String),
34    /// Connection pool error
35    Connection(String),
36    /// Transaction error
37    Transaction(String),
38    /// Schema error
39    Schema(String),
40    /// Query building error
41    Query(String),
42    /// Event system error
43    Event(String),
44    /// Configuration error
45    Configuration(String),
46    /// Invalid key error
47    InvalidKey(String),
48    /// Column not found error
49    ColumnNotFound(String),
50}
51
52impl fmt::Display for ModelError {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match self {
55            ModelError::Database(msg) => write!(f, "Database error: {}", msg),
56            ModelError::NotFound(table) => write!(f, "Record not found in table '{}'", table),
57            ModelError::Validation(msg) => write!(f, "Validation error: {}", msg),
58            ModelError::MissingPrimaryKey => write!(f, "Primary key is missing or invalid"),
59            ModelError::Relationship(msg) => write!(f, "Relationship error: {}", msg),
60            ModelError::Serialization(msg) => write!(f, "Serialization error: {}", msg),
61            ModelError::Migration(msg) => write!(f, "Migration error: {}", msg),
62            ModelError::Connection(msg) => write!(f, "Connection error: {}", msg),
63            ModelError::Transaction(msg) => write!(f, "Transaction error: {}", msg),
64            ModelError::Schema(msg) => write!(f, "Schema error: {}", msg),
65            ModelError::Query(msg) => write!(f, "Query error: {}", msg),
66            ModelError::Event(msg) => write!(f, "Event error: {}", msg),
67            ModelError::Configuration(msg) => write!(f, "Configuration error: {}", msg),
68            ModelError::InvalidKey(msg) => write!(f, "Invalid key error: {}", msg),
69            ModelError::ColumnNotFound(column) => write!(f, "Column not found: {}", column),
70        }
71    }
72}
73
74impl std::error::Error for ModelError {}
75
76// Convert from sqlx errors
77impl From<sqlx::Error> for ModelError {
78    fn from(err: sqlx::Error) -> Self {
79        ModelError::Database(err.to_string())
80    }
81}
82
83// Convert from serde_json errors
84impl From<serde_json::Error> for ModelError {
85    fn from(err: serde_json::Error) -> Self {
86        ModelError::Serialization(err.to_string())
87    }
88}
89
90// Convert from anyhow errors
91impl From<anyhow::Error> for ModelError {
92    fn from(err: anyhow::Error) -> Self {
93        ModelError::Database(err.to_string())
94    }
95}
96
97/// Error types for query builder operations
98#[derive(Debug, Clone)]
99pub enum QueryError {
100    /// Invalid SQL syntax
101    InvalidSql(String),
102    /// Missing required fields
103    MissingFields(String),
104    /// Invalid parameter binding
105    InvalidParameter(String),
106    /// Unsupported operation
107    UnsupportedOperation(String),
108}
109
110impl fmt::Display for QueryError {
111    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112        match self {
113            QueryError::InvalidSql(msg) => write!(f, "Invalid SQL: {}", msg),
114            QueryError::MissingFields(msg) => write!(f, "Missing fields: {}", msg),
115            QueryError::InvalidParameter(msg) => write!(f, "Invalid parameter: {}", msg),
116            QueryError::UnsupportedOperation(msg) => write!(f, "Unsupported operation: {}", msg),
117        }
118    }
119}
120
121impl std::error::Error for QueryError {}
122
123impl From<QueryError> for ModelError {
124    fn from(err: QueryError) -> Self {
125        ModelError::Query(err.to_string())
126    }
127}
128
129/// Error types for relationship operations
130#[derive(Debug, Clone)]
131pub enum RelationshipError {
132    /// Relationship not found
133    NotFound(String),
134    /// Invalid relationship configuration
135    InvalidConfiguration(String),
136    /// Circular dependency in relationships
137    CircularDependency(String),
138    /// Foreign key constraint violation
139    ForeignKeyViolation(String),
140}
141
142impl fmt::Display for RelationshipError {
143    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144        match self {
145            RelationshipError::NotFound(msg) => write!(f, "Relationship not found: {}", msg),
146            RelationshipError::InvalidConfiguration(msg) => {
147                write!(f, "Invalid relationship configuration: {}", msg)
148            }
149            RelationshipError::CircularDependency(msg) => write!(f, "Circular dependency: {}", msg),
150            RelationshipError::ForeignKeyViolation(msg) => {
151                write!(f, "Foreign key violation: {}", msg)
152            }
153        }
154    }
155}
156
157impl std::error::Error for RelationshipError {}
158
159impl From<RelationshipError> for ModelError {
160    fn from(err: RelationshipError) -> Self {
161        ModelError::Relationship(err.to_string())
162    }
163}
164
165/// Error types for migration operations
166#[derive(Debug, Clone)]
167pub enum MigrationError {
168    /// Migration file not found
169    FileNotFound(String),
170    /// Invalid migration syntax
171    InvalidSyntax(String),
172    /// Migration already applied
173    AlreadyApplied(String),
174    /// Migration rollback failed
175    RollbackFailed(String),
176    /// Version conflict
177    VersionConflict(String),
178}
179
180impl fmt::Display for MigrationError {
181    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182        match self {
183            MigrationError::FileNotFound(msg) => write!(f, "Migration file not found: {}", msg),
184            MigrationError::InvalidSyntax(msg) => write!(f, "Invalid migration syntax: {}", msg),
185            MigrationError::AlreadyApplied(msg) => write!(f, "Migration already applied: {}", msg),
186            MigrationError::RollbackFailed(msg) => write!(f, "Migration rollback failed: {}", msg),
187            MigrationError::VersionConflict(msg) => write!(f, "Version conflict: {}", msg),
188        }
189    }
190}
191
192impl std::error::Error for MigrationError {}
193
194impl From<MigrationError> for ModelError {
195    fn from(err: MigrationError) -> Self {
196        ModelError::Migration(err.to_string())
197    }
198}
199
200/// Error types for event system operations
201#[derive(Debug, Clone)]
202pub enum EventError {
203    /// Event handler failed
204    HandlerFailed(String),
205    /// Event propagation stopped
206    PropagationStopped(String),
207    /// Invalid event configuration
208    InvalidConfiguration(String),
209}
210
211impl fmt::Display for EventError {
212    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
213        match self {
214            EventError::HandlerFailed(msg) => write!(f, "Event handler failed: {}", msg),
215            EventError::PropagationStopped(msg) => write!(f, "Event propagation stopped: {}", msg),
216            EventError::InvalidConfiguration(msg) => {
217                write!(f, "Invalid event configuration: {}", msg)
218            }
219        }
220    }
221}
222
223impl std::error::Error for EventError {}
224
225impl From<EventError> for ModelError {
226    fn from(err: EventError) -> Self {
227        ModelError::Event(err.to_string())
228    }
229}