Skip to main content

schema_sync/
errors.rs

1//! Developer: s4gor
2//! Github: https://github.com/s4gor
3//!
4//! Error types for schema-sync
5//!
6//! All errors are structured to provide context about what operation failed
7//! and why, while maintaining tenant isolation information.
8
9use thiserror::Error;
10
11/// Result type alias for schema-sync operations
12pub type Result<T> = std::result::Result<T, Error>;
13
14/// Main error type for schema-sync
15#[derive(Error, Debug)]
16pub enum Error {
17    /// Database connection or query errors
18    #[error("Database error: {0}")]
19    Database(String),
20
21    /// Schema inspection errors
22    #[error("Schema inspection error: {0}")]
23    Inspection(String),
24
25    /// Migration execution errors
26    #[error("Migration error: {0}")]
27    Migration(String),
28
29    /// Tenant-related errors (isolation violations, missing tenant, etc.)
30    #[error("Tenant error: {0}")]
31    Tenant(String),
32
33    /// Planning errors (invalid plan, conflicts, etc.)
34    #[error("Planning error: {0}")]
35    Planning(String),
36
37    /// Execution errors (lock failures, rollback issues, etc.)
38    #[error("Execution error: {0}")]
39    Execution(String),
40
41    /// Snapshot errors (serialization, storage, etc.)
42    #[error("Snapshot error: {0}")]
43    Snapshot(String),
44
45    /// Diff calculation errors
46    #[error("Diff error: {0}")]
47    Diff(String),
48
49    /// Validation errors (CI mode failures)
50    #[error("Validation error: {0}")]
51    Validation(String),
52
53    /// IO errors
54    #[error("IO error: {0}")]
55    Io(#[from] std::io::Error),
56
57    /// Serialization errors
58    #[error("Serialization error: {0}")]
59    Serialization(#[from] serde_json::Error),
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_error_display() {
68        let err = Error::Database("Connection failed".to_string());
69        assert!(err.to_string().contains("Connection failed"));
70    }
71
72    #[test]
73    fn test_error_from_io() {
74        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
75        let err: Error = io_err.into();
76        assert!(err.to_string().contains("File not found"));
77    }
78}