Skip to main content

wasm_dbms_api/
error.rs

1use serde::{Deserialize, Serialize};
2use thiserror::Error;
3
4/// DBMS error type.
5#[derive(Debug, Error, Serialize, Deserialize)]
6#[cfg_attr(feature = "candid", derive(candid::CandidType))]
7pub enum DbmsError {
8    #[error("Memory error: {0}")]
9    Memory(#[from] crate::memory::MemoryError),
10    #[error("Migration error: {0}")]
11    Migration(#[from] crate::dbms::migration::MigrationError),
12    #[error("Query error: {0}")]
13    Query(#[from] crate::dbms::query::QueryError),
14    #[error("Sanitize error: {0}")]
15    Sanitize(String),
16    #[error("Table error: {0}")]
17    Table(#[from] crate::dbms::table::TableError),
18    #[error("Transaction error: {0}")]
19    Transaction(#[from] crate::dbms::transaction::TransactionError),
20    #[error("Validation error: {0}")]
21    Validation(String),
22}
23
24/// DBMS result type.
25pub type DbmsResult<T> = Result<T, DbmsError>;
26
27#[cfg(test)]
28mod test {
29
30    use super::*;
31    use crate::dbms::query::QueryError;
32    use crate::dbms::table::TableError;
33    use crate::dbms::transaction::TransactionError;
34    use crate::memory::MemoryError;
35
36    #[test]
37    fn test_should_display_memory_error() {
38        let error = DbmsError::Memory(MemoryError::OutOfBounds);
39        assert_eq!(
40            error.to_string(),
41            "Memory error: Stable memory access out of bounds"
42        );
43    }
44
45    #[test]
46    fn test_should_display_query_error() {
47        let error = DbmsError::Query(QueryError::UnknownColumn("foo".to_string()));
48        assert_eq!(error.to_string(), "Query error: Unknown column: foo");
49    }
50
51    #[test]
52    fn test_should_display_sanitize_error() {
53        let error = DbmsError::Sanitize("invalid input".to_string());
54        assert_eq!(error.to_string(), "Sanitize error: invalid input");
55    }
56
57    #[test]
58    fn test_should_display_table_error() {
59        let error = DbmsError::Table(TableError::TableNotFound);
60        assert_eq!(error.to_string(), "Table error: Table not found");
61    }
62
63    #[test]
64    fn test_should_display_transaction_error() {
65        let error = DbmsError::Transaction(TransactionError::NoActiveTransaction);
66        assert_eq!(
67            error.to_string(),
68            "Transaction error: No active transaction"
69        );
70    }
71
72    #[test]
73    fn test_should_display_validation_error() {
74        let error = DbmsError::Validation("invalid email".to_string());
75        assert_eq!(error.to_string(), "Validation error: invalid email");
76    }
77
78    #[test]
79    fn test_should_convert_from_memory_error() {
80        let error: DbmsError = MemoryError::OutOfBounds.into();
81        assert!(matches!(error, DbmsError::Memory(MemoryError::OutOfBounds)));
82    }
83
84    #[test]
85    fn test_should_convert_from_query_error() {
86        let error: DbmsError = QueryError::UnknownColumn("col".to_string()).into();
87        assert!(matches!(error, DbmsError::Query(_)));
88    }
89
90    #[test]
91    fn test_should_convert_from_table_error() {
92        let error: DbmsError = TableError::TableNotFound.into();
93        assert!(matches!(error, DbmsError::Table(_)));
94    }
95
96    #[test]
97    fn test_should_convert_from_transaction_error() {
98        let error: DbmsError = TransactionError::NoActiveTransaction.into();
99        assert!(matches!(error, DbmsError::Transaction(_)));
100    }
101}