| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
use goingson_core::CoreError; |
| 26 |
use serde::Serialize; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] |
| 33 |
#[serde(rename_all = "SCREAMING_SNAKE_CASE")] |
| 34 |
pub enum ErrorCode { |
| 35 |
|
| 36 |
NotFound, |
| 37 |
|
| 38 |
|
| 39 |
ValidationError, |
| 40 |
|
| 41 |
|
| 42 |
DatabaseError, |
| 43 |
|
| 44 |
|
| 45 |
BadRequest, |
| 46 |
|
| 47 |
|
| 48 |
AuthError, |
| 49 |
|
| 50 |
|
| 51 |
ParseError, |
| 52 |
|
| 53 |
|
| 54 |
InternalError, |
| 55 |
|
| 56 |
|
| 57 |
Conflict, |
| 58 |
|
| 59 |
|
| 60 |
ExternalServiceError, |
| 61 |
} |
| 62 |
|
| 63 |
impl ErrorCode { |
| 64 |
|
| 65 |
pub fn as_str(&self) -> &'static str { |
| 66 |
match self { |
| 67 |
ErrorCode::NotFound => "NOT_FOUND", |
| 68 |
ErrorCode::ValidationError => "VALIDATION_ERROR", |
| 69 |
ErrorCode::DatabaseError => "DATABASE_ERROR", |
| 70 |
ErrorCode::BadRequest => "BAD_REQUEST", |
| 71 |
ErrorCode::AuthError => "AUTH_ERROR", |
| 72 |
ErrorCode::ParseError => "PARSE_ERROR", |
| 73 |
ErrorCode::InternalError => "INTERNAL_ERROR", |
| 74 |
ErrorCode::Conflict => "CONFLICT", |
| 75 |
ErrorCode::ExternalServiceError => "EXTERNAL_SERVICE_ERROR", |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
#[derive(Debug, Clone, Serialize)] |
| 87 |
#[serde(rename_all = "camelCase")] |
| 88 |
pub struct ApiError { |
| 89 |
|
| 90 |
pub code: ErrorCode, |
| 91 |
|
| 92 |
|
| 93 |
pub message: String, |
| 94 |
|
| 95 |
|
| 96 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 97 |
pub details: Option<ErrorDetails>, |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
#[derive(Debug, Clone, Serialize)] |
| 102 |
#[serde(rename_all = "camelCase")] |
| 103 |
pub struct ErrorDetails { |
| 104 |
|
| 105 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 106 |
pub field: Option<String>, |
| 107 |
|
| 108 |
|
| 109 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 110 |
pub resource: Option<String>, |
| 111 |
|
| 112 |
|
| 113 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 114 |
pub resource_id: Option<String>, |
| 115 |
} |
| 116 |
|
| 117 |
impl ApiError { |
| 118 |
|
| 119 |
pub fn new(code: ErrorCode, message: impl Into<String>) -> Self { |
| 120 |
Self { |
| 121 |
code, |
| 122 |
message: message.into(), |
| 123 |
details: None, |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
pub fn with_details(code: ErrorCode, message: impl Into<String>, details: ErrorDetails) -> Self { |
| 129 |
Self { |
| 130 |
code, |
| 131 |
message: message.into(), |
| 132 |
details: Some(details), |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
pub fn not_found(resource: &str, id: impl ToString) -> Self { |
| 140 |
let id_str = id.to_string(); |
| 141 |
Self::with_details( |
| 142 |
ErrorCode::NotFound, |
| 143 |
format!("{} not found: {}", resource, id_str), |
| 144 |
ErrorDetails { |
| 145 |
field: None, |
| 146 |
resource: Some(resource.to_string()), |
| 147 |
resource_id: Some(id_str), |
| 148 |
}, |
| 149 |
) |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
pub fn validation(field: &str, message: impl Into<String>) -> Self { |
| 154 |
Self::with_details( |
| 155 |
ErrorCode::ValidationError, |
| 156 |
message.into(), |
| 157 |
ErrorDetails { |
| 158 |
field: Some(field.to_string()), |
| 159 |
resource: None, |
| 160 |
resource_id: None, |
| 161 |
}, |
| 162 |
) |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
pub fn validation_msg(message: impl Into<String>) -> Self { |
| 167 |
Self::new(ErrorCode::ValidationError, message) |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
pub fn database(message: impl Into<String>) -> Self { |
| 172 |
Self::new(ErrorCode::DatabaseError, message) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
pub fn bad_request(message: impl Into<String>) -> Self { |
| 177 |
Self::new(ErrorCode::BadRequest, message) |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
pub fn auth(message: impl Into<String>) -> Self { |
| 182 |
Self::new(ErrorCode::AuthError, message) |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
pub fn parse(message: impl Into<String>) -> Self { |
| 187 |
Self::new(ErrorCode::ParseError, message) |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
pub fn internal(message: impl Into<String>) -> Self { |
| 192 |
Self::new(ErrorCode::InternalError, message) |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
pub fn conflict(message: impl Into<String>) -> Self { |
| 197 |
Self::new(ErrorCode::Conflict, message) |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
pub fn external_service(message: impl Into<String>) -> Self { |
| 202 |
Self::new(ErrorCode::ExternalServiceError, message) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
pub trait OptionNotFound<T> { |
| 222 |
|
| 223 |
fn or_not_found(self, entity: &str, id: impl ToString) -> Result<T, ApiError>; |
| 224 |
} |
| 225 |
|
| 226 |
impl<T> OptionNotFound<T> for Option<T> { |
| 227 |
fn or_not_found(self, entity: &str, id: impl ToString) -> Result<T, ApiError> { |
| 228 |
self.ok_or_else(|| ApiError::not_found(entity, id)) |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
pub trait OptionApiError<T> { |
| 246 |
|
| 247 |
fn or_api_err(self, f: impl FnOnce() -> ApiError) -> Result<T, ApiError>; |
| 248 |
} |
| 249 |
|
| 250 |
impl<T> OptionApiError<T> for Option<T> { |
| 251 |
fn or_api_err(self, f: impl FnOnce() -> ApiError) -> Result<T, ApiError> { |
| 252 |
self.ok_or_else(f) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
pub trait ResultApiError<T, E: std::fmt::Display> { |
| 272 |
|
| 273 |
fn map_api_err( |
| 274 |
self, |
| 275 |
prefix: &str, |
| 276 |
constructor: fn(String) -> ApiError, |
| 277 |
) -> Result<T, ApiError>; |
| 278 |
} |
| 279 |
|
| 280 |
impl<T, E: std::fmt::Display> ResultApiError<T, E> for Result<T, E> { |
| 281 |
fn map_api_err( |
| 282 |
self, |
| 283 |
prefix: &str, |
| 284 |
constructor: fn(String) -> ApiError, |
| 285 |
) -> Result<T, ApiError> { |
| 286 |
self.map_err(|e| constructor(format!("{}: {}", prefix, e))) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
impl std::fmt::Display for ApiError { |
| 291 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 292 |
write!(f, "[{}] {}", self.code.as_str(), self.message) |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
impl std::error::Error for ApiError {} |
| 297 |
|
| 298 |
|
| 299 |
impl From<CoreError> for ApiError { |
| 300 |
fn from(err: CoreError) -> Self { |
| 301 |
match err { |
| 302 |
CoreError::NotFound { resource, id } => ApiError::with_details( |
| 303 |
ErrorCode::NotFound, |
| 304 |
format!("{} not found: {}", resource, id), |
| 305 |
ErrorDetails { |
| 306 |
field: None, |
| 307 |
resource: Some(resource.to_string()), |
| 308 |
resource_id: Some(id), |
| 309 |
}, |
| 310 |
), |
| 311 |
CoreError::Validation { field, message } => ApiError::with_details( |
| 312 |
ErrorCode::ValidationError, |
| 313 |
message, |
| 314 |
ErrorDetails { |
| 315 |
field: Some(field.to_string()), |
| 316 |
resource: None, |
| 317 |
resource_id: None, |
| 318 |
}, |
| 319 |
), |
| 320 |
CoreError::Database { message, .. } => ApiError::database(message), |
| 321 |
CoreError::Parse(message) => ApiError::parse(message), |
| 322 |
CoreError::BadRequest(message) => ApiError::bad_request(message), |
| 323 |
CoreError::Internal(message) => ApiError::internal(message), |
| 324 |
CoreError::Auth(message) => ApiError::auth(message), |
| 325 |
CoreError::Sync(message) => ApiError::external_service(message), |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
#[cfg(test)] |
| 331 |
mod tests { |
| 332 |
use super::*; |
| 333 |
|
| 334 |
#[test] |
| 335 |
fn test_not_found_error() { |
| 336 |
let err = ApiError::not_found("task", "123e4567-e89b-12d3-a456-426614174000"); |
| 337 |
|
| 338 |
assert_eq!(err.code, ErrorCode::NotFound); |
| 339 |
assert!(err.message.contains("task")); |
| 340 |
assert!(err.message.contains("123e4567-e89b-12d3-a456-426614174000")); |
| 341 |
|
| 342 |
let details = err.details.unwrap(); |
| 343 |
assert_eq!(details.resource, Some("task".to_string())); |
| 344 |
assert_eq!( |
| 345 |
details.resource_id, |
| 346 |
Some("123e4567-e89b-12d3-a456-426614174000".to_string()) |
| 347 |
); |
| 348 |
} |
| 349 |
|
| 350 |
#[test] |
| 351 |
fn test_validation_error() { |
| 352 |
let err = ApiError::validation("description", "Description is required"); |
| 353 |
|
| 354 |
assert_eq!(err.code, ErrorCode::ValidationError); |
| 355 |
assert_eq!(err.message, "Description is required"); |
| 356 |
|
| 357 |
let details = err.details.unwrap(); |
| 358 |
assert_eq!(details.field, Some("description".to_string())); |
| 359 |
} |
| 360 |
|
| 361 |
#[test] |
| 362 |
fn test_core_error_conversion() { |
| 363 |
let core_err = CoreError::not_found("project", "abc123"); |
| 364 |
let api_err: ApiError = core_err.into(); |
| 365 |
|
| 366 |
assert_eq!(api_err.code, ErrorCode::NotFound); |
| 367 |
assert!(api_err.message.contains("project")); |
| 368 |
assert!(api_err.message.contains("abc123")); |
| 369 |
} |
| 370 |
|
| 371 |
#[test] |
| 372 |
fn test_error_display() { |
| 373 |
let err = ApiError::database("Connection failed"); |
| 374 |
assert_eq!(err.to_string(), "[DATABASE_ERROR] Connection failed"); |
| 375 |
} |
| 376 |
|
| 377 |
#[test] |
| 378 |
fn test_serialization() { |
| 379 |
let err = ApiError::validation("email", "Invalid email format"); |
| 380 |
let json = serde_json::to_string(&err).unwrap(); |
| 381 |
|
| 382 |
|
| 383 |
assert!(json.contains("\"code\":\"VALIDATION_ERROR\"")); |
| 384 |
assert!(json.contains("\"message\":\"Invalid email format\"")); |
| 385 |
assert!(json.contains("\"field\":\"email\"")); |
| 386 |
} |
| 387 |
|
| 388 |
#[test] |
| 389 |
fn test_details_skip_none() { |
| 390 |
let err = ApiError::internal("Something went wrong"); |
| 391 |
let json = serde_json::to_string(&err).unwrap(); |
| 392 |
|
| 393 |
|
| 394 |
assert!(!json.contains("details")); |
| 395 |
} |
| 396 |
} |
| 397 |
|