1use std::fmt;
7
8pub type ModelResult<T> = Result<T, ModelError>;
10
11pub type OrmError = ModelError;
13
14pub type OrmResult<T> = ModelResult<T>;
16
17#[derive(Debug, Clone)]
19pub enum ModelError {
20 Database(String),
22 NotFound(String),
24 Validation(String),
26 MissingPrimaryKey,
28 Relationship(String),
30 Serialization(String),
32 Migration(String),
34 Connection(String),
36 Transaction(String),
38 Schema(String),
40 Query(String),
42 Event(String),
44 Configuration(String),
46 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
73impl From<sqlx::Error> for ModelError {
75 fn from(err: sqlx::Error) -> Self {
76 ModelError::Database(err.to_string())
77 }
78}
79
80impl From<serde_json::Error> for ModelError {
82 fn from(err: serde_json::Error) -> Self {
83 ModelError::Serialization(err.to_string())
84 }
85}
86
87impl From<anyhow::Error> for ModelError {
89 fn from(err: anyhow::Error) -> Self {
90 ModelError::Database(err.to_string())
91 }
92}
93
94#[derive(Debug, Clone)]
96pub enum QueryError {
97 InvalidSql(String),
99 MissingFields(String),
101 InvalidParameter(String),
103 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#[derive(Debug, Clone)]
128pub enum RelationshipError {
129 NotFound(String),
131 InvalidConfiguration(String),
133 CircularDependency(String),
135 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#[derive(Debug, Clone)]
160pub enum MigrationError {
161 FileNotFound(String),
163 InvalidSyntax(String),
165 AlreadyApplied(String),
167 RollbackFailed(String),
169 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#[derive(Debug, Clone)]
195pub enum EventError {
196 HandlerFailed(String),
198 PropagationStopped(String),
200 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}