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 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
76impl From<sqlx::Error> for ModelError {
78 fn from(err: sqlx::Error) -> Self {
79 ModelError::Database(err.to_string())
80 }
81}
82
83impl From<serde_json::Error> for ModelError {
85 fn from(err: serde_json::Error) -> Self {
86 ModelError::Serialization(err.to_string())
87 }
88}
89
90impl From<anyhow::Error> for ModelError {
92 fn from(err: anyhow::Error) -> Self {
93 ModelError::Database(err.to_string())
94 }
95}
96
97#[derive(Debug, Clone)]
99pub enum QueryError {
100 InvalidSql(String),
102 MissingFields(String),
104 InvalidParameter(String),
106 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#[derive(Debug, Clone)]
131pub enum RelationshipError {
132 NotFound(String),
134 InvalidConfiguration(String),
136 CircularDependency(String),
138 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#[derive(Debug, Clone)]
167pub enum MigrationError {
168 FileNotFound(String),
170 InvalidSyntax(String),
172 AlreadyApplied(String),
174 RollbackFailed(String),
176 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#[derive(Debug, Clone)]
202pub enum EventError {
203 HandlerFailed(String),
205 PropagationStopped(String),
207 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}