1use candid::CandidType;
2use serde::{Deserialize, Serialize};
3use thiserror::Error;
4
5#[derive(Debug, Error, CandidType, Serialize, Deserialize)]
7pub enum IcDbmsError {
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
22pub type IcDbmsResult<T> = Result<T, IcDbmsError>;
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 = IcDbmsError::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 = IcDbmsError::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 = IcDbmsError::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 = IcDbmsError::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 = IcDbmsError::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 = IcDbmsError::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: IcDbmsError = MemoryError::OutOfBounds.into();
79 assert!(matches!(
80 error,
81 IcDbmsError::Memory(MemoryError::OutOfBounds)
82 ));
83 }
84
85 #[test]
86 fn test_should_convert_from_query_error() {
87 let error: IcDbmsError = QueryError::UnknownColumn("col".to_string()).into();
88 assert!(matches!(error, IcDbmsError::Query(_)));
89 }
90
91 #[test]
92 fn test_should_convert_from_table_error() {
93 let error: IcDbmsError = TableError::TableNotFound.into();
94 assert!(matches!(error, IcDbmsError::Table(_)));
95 }
96
97 #[test]
98 fn test_should_convert_from_transaction_error() {
99 let error: IcDbmsError = TransactionError::NoActiveTransaction.into();
100 assert!(matches!(error, IcDbmsError::Transaction(_)));
101 }
102}