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}
49
50impl fmt::Display for ModelError {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        match self {
53            ModelError::Database(msg) => write!(f, "Database error: {}", msg),
54            ModelError::NotFound(table) => write!(f, "Record not found in table '{}'", table),
55            ModelError::Validation(msg) => write!(f, "Validation error: {}", msg),
56            ModelError::MissingPrimaryKey => write!(f, "Primary key is missing or invalid"),
57            ModelError::Relationship(msg) => write!(f, "Relationship error: {}", msg),
58            ModelError::Serialization(msg) => write!(f, "Serialization error: {}", msg),
59            ModelError::Migration(msg) => write!(f, "Migration error: {}", msg),
60            ModelError::Connection(msg) => write!(f, "Connection error: {}", msg),
61            ModelError::Transaction(msg) => write!(f, "Transaction error: {}", msg),
62            ModelError::Schema(msg) => write!(f, "Schema error: {}", msg),
63            ModelError::Query(msg) => write!(f, "Query error: {}", msg),
64            ModelError::Event(msg) => write!(f, "Event error: {}", msg),
65            ModelError::Configuration(msg) => write!(f, "Configuration error: {}", msg),
66            ModelError::InvalidKey(msg) => write!(f, "Invalid key error: {}", msg),
67        }
68    }
69}
70
71impl std::error::Error for ModelError {}
72
73// Convert from sqlx errors
74impl From<sqlx::Error> for ModelError {
75    fn from(err: sqlx::Error) -> Self {
76        ModelError::Database(err.to_string())
77    }
78}
79
80// Convert from serde_json errors
81impl From<serde_json::Error> for ModelError {
82    fn from(err: serde_json::Error) -> Self {
83        ModelError::Serialization(err.to_string())
84    }
85}
86
87// Convert from anyhow errors
88impl From<anyhow::Error> for ModelError {
89    fn from(err: anyhow::Error) -> Self {
90        ModelError::Database(err.to_string())
91    }
92}
93
94/// Error types for query builder operations
95#[derive(Debug, Clone)]
96pub enum QueryError {
97    /// Invalid SQL syntax
98    InvalidSql(String),
99    /// Missing required fields
100    MissingFields(String),
101    /// Invalid parameter binding
102    InvalidParameter(String),
103    /// Unsupported operation
104    UnsupportedOperation(String),
105}
106
107impl fmt::Display for QueryError {
108    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109        match self {
110            QueryError::InvalidSql(msg) => write!(f, "Invalid SQL: {}", msg),
111            QueryError::MissingFields(msg) => write!(f, "Missing fields: {}", msg),
112            QueryError::InvalidParameter(msg) => write!(f, "Invalid parameter: {}", msg),
113            QueryError::UnsupportedOperation(msg) => write!(f, "Unsupported operation: {}", msg),
114        }
115    }
116}
117
118impl std::error::Error for QueryError {}
119
120impl From<QueryError> for ModelError {
121    fn from(err: QueryError) -> Self {
122        ModelError::Query(err.to_string())
123    }
124}
125
126/// Error types for relationship operations
127#[derive(Debug, Clone)]
128pub enum RelationshipError {
129    /// Relationship not found
130    NotFound(String),
131    /// Invalid relationship configuration
132    InvalidConfiguration(String),
133    /// Circular dependency in relationships
134    CircularDependency(String),
135    /// Foreign key constraint violation
136    ForeignKeyViolation(String),
137}
138
139impl fmt::Display for RelationshipError {
140    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141        match self {
142            RelationshipError::NotFound(msg) => write!(f, "Relationship not found: {}", msg),
143            RelationshipError::InvalidConfiguration(msg) => write!(f, "Invalid relationship configuration: {}", msg),
144            RelationshipError::CircularDependency(msg) => write!(f, "Circular dependency: {}", msg),
145            RelationshipError::ForeignKeyViolation(msg) => write!(f, "Foreign key violation: {}", msg),
146        }
147    }
148}
149
150impl std::error::Error for RelationshipError {}
151
152impl From<RelationshipError> for ModelError {
153    fn from(err: RelationshipError) -> Self {
154        ModelError::Relationship(err.to_string())
155    }
156}
157
158/// Error types for migration operations
159#[derive(Debug, Clone)]
160pub enum MigrationError {
161    /// Migration file not found
162    FileNotFound(String),
163    /// Invalid migration syntax
164    InvalidSyntax(String),
165    /// Migration already applied
166    AlreadyApplied(String),
167    /// Migration rollback failed
168    RollbackFailed(String),
169    /// Version conflict
170    VersionConflict(String),
171}
172
173impl fmt::Display for MigrationError {
174    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175        match self {
176            MigrationError::FileNotFound(msg) => write!(f, "Migration file not found: {}", msg),
177            MigrationError::InvalidSyntax(msg) => write!(f, "Invalid migration syntax: {}", msg),
178            MigrationError::AlreadyApplied(msg) => write!(f, "Migration already applied: {}", msg),
179            MigrationError::RollbackFailed(msg) => write!(f, "Migration rollback failed: {}", msg),
180            MigrationError::VersionConflict(msg) => write!(f, "Version conflict: {}", msg),
181        }
182    }
183}
184
185impl std::error::Error for MigrationError {}
186
187impl From<MigrationError> for ModelError {
188    fn from(err: MigrationError) -> Self {
189        ModelError::Migration(err.to_string())
190    }
191}
192
193/// Error types for event system operations
194#[derive(Debug, Clone)]
195pub enum EventError {
196    /// Event handler failed
197    HandlerFailed(String),
198    /// Event propagation stopped
199    PropagationStopped(String),
200    /// Invalid event configuration
201    InvalidConfiguration(String),
202}
203
204impl fmt::Display for EventError {
205    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
206        match self {
207            EventError::HandlerFailed(msg) => write!(f, "Event handler failed: {}", msg),
208            EventError::PropagationStopped(msg) => write!(f, "Event propagation stopped: {}", msg),
209            EventError::InvalidConfiguration(msg) => write!(f, "Invalid event configuration: {}", msg),
210        }
211    }
212}
213
214impl std::error::Error for EventError {}
215
216impl From<EventError> for ModelError {
217    fn from(err: EventError) -> Self {
218        ModelError::Event(err.to_string())
219    }
220}