Skip to main content

keradb_sdk/
error.rs

1//! Error types for the KeraDB Rust SDK.
2
3// @group ErrorTypes : Error definitions and conversions for KeraDB SDK
4
5use thiserror::Error;
6
7/// Primary error type for all KeraDB operations.
8#[derive(Debug, Error)]
9pub enum KeraDbError {
10    /// The native library could not be loaded.
11    #[error("Failed to load KeraDB native library: {0}")]
12    LibraryLoad(String),
13
14    /// A native FFI call returned an error string.
15    #[error("KeraDB error: {0}")]
16    Native(String),
17
18    /// A JSON serialization or deserialization error.
19    #[error("JSON error: {0}")]
20    Json(#[from] serde_json::Error),
21
22    /// A string contained invalid UTF-8.
23    #[error("UTF-8 encoding error: {0}")]
24    Utf8(String),
25
26    /// A null pointer was returned where a valid pointer was expected.
27    #[error("Null pointer returned from native call: {0}")]
28    NullPointer(String),
29
30    /// An operation was attempted on a closed database.
31    #[error("Database is closed")]
32    Closed,
33
34    /// A generic, context-free error string.
35    #[error("{0}")]
36    Other(String),
37}
38
39/// Convenience alias used throughout the crate.
40pub type Result<T> = std::result::Result<T, KeraDbError>;
41
42// ---------------------------------------------------------------------------
43// @group UnitTests : Error type formatting and conversions
44// ---------------------------------------------------------------------------
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn library_load_error_message() {
52        let e = KeraDbError::LibraryLoad("not found".into());
53        assert!(e.to_string().contains("not found"));
54    }
55
56    #[test]
57    fn native_error_message() {
58        let e = KeraDbError::Native("disk full".into());
59        assert_eq!(e.to_string(), "KeraDB error: disk full");
60    }
61
62    #[test]
63    fn json_error_from_serde() {
64        let raw = serde_json::from_str::<serde_json::Value>("{bad");
65        let e: KeraDbError = raw.unwrap_err().into();
66        assert!(e.to_string().contains("JSON error"));
67    }
68
69    #[test]
70    fn utf8_error_message() {
71        let e = KeraDbError::Utf8("invalid sequence".into());
72        assert!(e.to_string().contains("UTF-8"));
73    }
74
75    #[test]
76    fn null_pointer_error_message() {
77        let e = KeraDbError::NullPointer("insert returned null".into());
78        assert!(e.to_string().contains("Null pointer"));
79    }
80
81    #[test]
82    fn closed_error_message() {
83        let e = KeraDbError::Closed;
84        assert_eq!(e.to_string(), "Database is closed");
85    }
86
87    #[test]
88    fn other_error_message() {
89        let e = KeraDbError::Other("something went wrong".into());
90        assert_eq!(e.to_string(), "something went wrong");
91    }
92
93    #[test]
94    fn result_ok_propagates() {
95        let r: Result<i32> = Ok(42);
96        assert_eq!(r.unwrap(), 42);
97    }
98
99    #[test]
100    fn result_err_propagates() {
101        let r: Result<i32> = Err(KeraDbError::Closed);
102        assert!(r.is_err());
103    }
104}