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