libsql_orm/error.rs
1//! Error handling for libsql-orm
2//!
3//! This module provides comprehensive error types and handling for all operations
4//! within the libsql-orm library. All errors implement the standard `Error` trait
5//! and provide detailed error messages for debugging.
6//!
7//! # Error Categories
8//!
9//! - **Connection Errors**: Database connection failures
10//! - **SQL Errors**: Query execution problems
11//! - **Serialization Errors**: Data conversion issues
12//! - **Validation Errors**: Data validation failures
13//! - **Not Found Errors**: Resource not found
14//! - **Pagination Errors**: Pagination parameter issues
15//! - **Query Errors**: Query building problems
16//!
17//! # Examples
18//!
19//! ```rust
20//! use libsql_orm::{Error, Result};
21//!
22//! fn handle_error(result: Result<String>) {
23//! match result {
24//! Ok(value) => println!("Success: {}", value),
25//! Err(Error::NotFound(msg)) => println!("Resource not found: {}", msg),
26//! Err(Error::Validation(msg)) => println!("Validation failed: {}", msg),
27//! Err(e) => println!("Other error: {}", e),
28//! }
29//! }
30//! ```
31
32use std::fmt;
33
34/// Custom error type for the libsql-orm crate
35///
36/// Provides comprehensive error handling for all database and ORM operations.
37/// All variants include descriptive messages to aid in debugging and error handling.
38#[derive(Debug)]
39pub enum Error {
40 /// Database connection error
41 Connection(String),
42 /// SQL execution error
43 Sql(String),
44 /// Serialization/deserialization error
45 Serialization(String),
46 /// Validation error
47 Validation(String),
48 /// Not found error
49 NotFound(String),
50 /// Pagination error
51 Pagination(String),
52 /// Query building error
53 Query(String),
54 /// Worker environment error
55 AnyhowError(String),
56 /// Database error
57 DatabaseError(String),
58 /// Generic error
59 Generic(String),
60}
61
62impl std::error::Error for Error {}
63
64impl fmt::Display for Error {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 match self {
67 Error::Connection(msg) => write!(f, "Connection error: {msg}"),
68 Error::Sql(msg) => write!(f, "SQL error: {msg}"),
69 Error::Serialization(msg) => write!(f, "Serialization error: {msg}"),
70 Error::Validation(msg) => write!(f, "Validation error: {msg}"),
71 Error::NotFound(msg) => write!(f, "Not found: {msg}"),
72 Error::Pagination(msg) => write!(f, "Pagination error: {msg}"),
73 Error::Query(msg) => write!(f, "Query error: {msg}"),
74 Error::AnyhowError(msg) => write!(f, "Anyhow error: {msg}"),
75 Error::DatabaseError(msg) => write!(f, "Database error: {msg}"),
76 Error::Generic(msg) => write!(f, "Error: {msg}"),
77 }
78 }
79}
80
81impl From<libsql::Error> for Error {
82 fn from(err: libsql::Error) -> Self {
83 Error::Sql(err.to_string())
84 }
85}
86
87impl From<serde_json::Error> for Error {
88 fn from(err: serde_json::Error) -> Self {
89 Error::Serialization(err.to_string())
90 }
91}
92
93impl From<std::io::Error> for Error {
94 fn from(err: std::io::Error) -> Self {
95 Error::Generic(err.to_string())
96 }
97}
98
99impl From<Box<dyn std::error::Error + Send + Sync>> for Error {
100 fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
101 Error::Generic(err.to_string())
102 }
103}
104
105impl From<anyhow::Error> for Error {
106 fn from(err: anyhow::Error) -> Self {
107 Error::AnyhowError(err.to_string())
108 }
109}
110
111/// Result type alias for the crate
112pub type Result<T> = std::result::Result<T, Error>;