1use std::fmt;
7
8pub type ModelResult<T> = Result<T, ModelError>;
10
11#[derive(Debug, Clone)]
13pub enum ModelError {
14 Database(String),
16 NotFound(String),
18 Validation(String),
20 MissingPrimaryKey,
22 Relationship(String),
24 Serialization(String),
26 Migration(String),
28 Connection(String),
30 Transaction(String),
32 Schema(String),
34 Query(String),
36 Event(String),
38}
39
40impl fmt::Display for ModelError {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 match self {
43 ModelError::Database(msg) => write!(f, "Database error: {}", msg),
44 ModelError::NotFound(table) => write!(f, "Record not found in table '{}'", table),
45 ModelError::Validation(msg) => write!(f, "Validation error: {}", msg),
46 ModelError::MissingPrimaryKey => write!(f, "Primary key is missing or invalid"),
47 ModelError::Relationship(msg) => write!(f, "Relationship error: {}", msg),
48 ModelError::Serialization(msg) => write!(f, "Serialization error: {}", msg),
49 ModelError::Migration(msg) => write!(f, "Migration error: {}", msg),
50 ModelError::Connection(msg) => write!(f, "Connection error: {}", msg),
51 ModelError::Transaction(msg) => write!(f, "Transaction error: {}", msg),
52 ModelError::Schema(msg) => write!(f, "Schema error: {}", msg),
53 ModelError::Query(msg) => write!(f, "Query error: {}", msg),
54 ModelError::Event(msg) => write!(f, "Event error: {}", msg),
55 }
56 }
57}
58
59impl std::error::Error for ModelError {}
60
61impl From<sqlx::Error> for ModelError {
63 fn from(err: sqlx::Error) -> Self {
64 ModelError::Database(err.to_string())
65 }
66}
67
68impl From<serde_json::Error> for ModelError {
70 fn from(err: serde_json::Error) -> Self {
71 ModelError::Serialization(err.to_string())
72 }
73}
74
75impl From<anyhow::Error> for ModelError {
77 fn from(err: anyhow::Error) -> Self {
78 ModelError::Database(err.to_string())
79 }
80}
81
82#[derive(Debug, Clone)]
84pub enum QueryError {
85 InvalidSql(String),
87 MissingFields(String),
89 InvalidParameter(String),
91 UnsupportedOperation(String),
93}
94
95impl fmt::Display for QueryError {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 match self {
98 QueryError::InvalidSql(msg) => write!(f, "Invalid SQL: {}", msg),
99 QueryError::MissingFields(msg) => write!(f, "Missing fields: {}", msg),
100 QueryError::InvalidParameter(msg) => write!(f, "Invalid parameter: {}", msg),
101 QueryError::UnsupportedOperation(msg) => write!(f, "Unsupported operation: {}", msg),
102 }
103 }
104}
105
106impl std::error::Error for QueryError {}
107
108impl From<QueryError> for ModelError {
109 fn from(err: QueryError) -> Self {
110 ModelError::Query(err.to_string())
111 }
112}
113
114#[derive(Debug, Clone)]
116pub enum RelationshipError {
117 NotFound(String),
119 InvalidConfiguration(String),
121 CircularDependency(String),
123 ForeignKeyViolation(String),
125}
126
127impl fmt::Display for RelationshipError {
128 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129 match self {
130 RelationshipError::NotFound(msg) => write!(f, "Relationship not found: {}", msg),
131 RelationshipError::InvalidConfiguration(msg) => write!(f, "Invalid relationship configuration: {}", msg),
132 RelationshipError::CircularDependency(msg) => write!(f, "Circular dependency: {}", msg),
133 RelationshipError::ForeignKeyViolation(msg) => write!(f, "Foreign key violation: {}", msg),
134 }
135 }
136}
137
138impl std::error::Error for RelationshipError {}
139
140impl From<RelationshipError> for ModelError {
141 fn from(err: RelationshipError) -> Self {
142 ModelError::Relationship(err.to_string())
143 }
144}
145
146#[derive(Debug, Clone)]
148pub enum MigrationError {
149 FileNotFound(String),
151 InvalidSyntax(String),
153 AlreadyApplied(String),
155 RollbackFailed(String),
157 VersionConflict(String),
159}
160
161impl fmt::Display for MigrationError {
162 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163 match self {
164 MigrationError::FileNotFound(msg) => write!(f, "Migration file not found: {}", msg),
165 MigrationError::InvalidSyntax(msg) => write!(f, "Invalid migration syntax: {}", msg),
166 MigrationError::AlreadyApplied(msg) => write!(f, "Migration already applied: {}", msg),
167 MigrationError::RollbackFailed(msg) => write!(f, "Migration rollback failed: {}", msg),
168 MigrationError::VersionConflict(msg) => write!(f, "Version conflict: {}", msg),
169 }
170 }
171}
172
173impl std::error::Error for MigrationError {}
174
175impl From<MigrationError> for ModelError {
176 fn from(err: MigrationError) -> Self {
177 ModelError::Migration(err.to_string())
178 }
179}
180
181#[derive(Debug, Clone)]
183pub enum EventError {
184 HandlerFailed(String),
186 PropagationStopped(String),
188 InvalidConfiguration(String),
190}
191
192impl fmt::Display for EventError {
193 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
194 match self {
195 EventError::HandlerFailed(msg) => write!(f, "Event handler failed: {}", msg),
196 EventError::PropagationStopped(msg) => write!(f, "Event propagation stopped: {}", msg),
197 EventError::InvalidConfiguration(msg) => write!(f, "Invalid event configuration: {}", msg),
198 }
199 }
200}
201
202impl std::error::Error for EventError {}
203
204impl From<EventError> for ModelError {
205 fn from(err: EventError) -> Self {
206 ModelError::Event(err.to_string())
207 }
208}