Skip to main content

mongreldb_kit/
error.rs

1//! Error model for `mongreldb-kit`.
2//!
3//! Storage errors from MongrelDB core and validation errors from the core model
4//! are folded into a small, stable set of categories so consumers can handle
5//! failures without depending on internal crate details.
6
7use thiserror::Error;
8
9pub type Result<T> = std::result::Result<T, KitError>;
10
11/// A storage/transaction error in kit terminology.
12#[derive(Debug, Error, Clone, PartialEq)]
13pub enum KitError {
14    #[error("validation error: {0}")]
15    Validation(String),
16    #[error("duplicate key: {0}")]
17    Duplicate(String),
18    #[error("foreign key violation: {0}")]
19    ForeignKey(String),
20    #[error("restrict violation: {0}")]
21    Restrict(String),
22    #[error("migration error: {0}")]
23    Migration(String),
24    #[error("conflict: {0}")]
25    Conflict(String),
26    #[error("storage error: {0}")]
27    Storage(String),
28    #[error("integrity error: {0}")]
29    Integrity(String),
30}
31
32impl From<std::io::Error> for KitError {
33    fn from(e: std::io::Error) -> Self {
34        KitError::Storage(e.to_string())
35    }
36}
37
38impl From<mongreldb_core::MongrelError> for KitError {
39    fn from(e: mongreldb_core::MongrelError) -> Self {
40        use mongreldb_core::MongrelError;
41        match e {
42            MongrelError::Conflict(msg) => KitError::Conflict(msg),
43            MongrelError::InvalidArgument(msg) => KitError::Validation(msg),
44            MongrelError::Schema(msg) => KitError::Validation(msg),
45            MongrelError::ColumnNotFound(msg) => KitError::Integrity(msg),
46            MongrelError::NotFound(msg) => KitError::Integrity(msg),
47            MongrelError::Io(e) => KitError::Storage(e.to_string()),
48            MongrelError::Serialization(e) => KitError::Storage(e.to_string()),
49            MongrelError::ChecksumMismatch { .. }
50            | MongrelError::MagicMismatch { .. }
51            | MongrelError::CorruptWal { .. }
52            | MongrelError::TornWrite { .. } => KitError::Integrity(e.to_string()),
53            MongrelError::EncryptionDisabled
54            | MongrelError::Encryption(_)
55            | MongrelError::Decryption(_) => KitError::Integrity(e.to_string()),
56            MongrelError::Full(msg) => KitError::Storage(msg),
57            MongrelError::Other(msg) => KitError::Storage(msg),
58            _ => KitError::Storage(e.to_string()),
59        }
60    }
61}
62
63impl From<mongreldb_kit_core::schema::SchemaError> for KitError {
64    fn from(e: mongreldb_kit_core::schema::SchemaError) -> Self {
65        KitError::Validation(e.to_string())
66    }
67}
68
69impl From<mongreldb_kit_core::validation::ValidationError> for KitError {
70    fn from(e: mongreldb_kit_core::validation::ValidationError) -> Self {
71        KitError::Validation(e.to_string())
72    }
73}
74
75impl From<mongreldb_kit_core::planner::PlannerError> for KitError {
76    fn from(e: mongreldb_kit_core::planner::PlannerError) -> Self {
77        match e {
78            mongreldb_kit_core::planner::PlannerError::TableNotFound(msg) => {
79                KitError::Integrity(msg)
80            }
81            mongreldb_kit_core::planner::PlannerError::CircularDelete(msg) => {
82                KitError::Restrict(msg)
83            }
84        }
85    }
86}
87
88impl From<serde_json::Error> for KitError {
89    fn from(e: serde_json::Error) -> Self {
90        KitError::Storage(e.to_string())
91    }
92}