Skip to main content

uqa_graph/store/
error.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Retain provider diagnostics through cloneable graph errors.
8
9use std::{error::Error, fmt, sync::Arc};
10use uqa_storage::StorageBackendError;
11
12/// A retained storage cause. Clones share the original error; equality compares that retained identity, not its display text.
13#[derive(Debug, Clone)]
14pub struct GraphStorageError(Arc<StorageBackendError>);
15
16impl From<StorageBackendError> for GraphStorageError {
17    fn from(error: StorageBackendError) -> Self {
18        Self(Arc::new(error))
19    }
20}
21
22impl PartialEq for GraphStorageError {
23    fn eq(&self, other: &Self) -> bool {
24        Arc::ptr_eq(&self.0, &other.0)
25    }
26}
27
28impl Eq for GraphStorageError {}
29
30impl fmt::Display for GraphStorageError {
31    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
32        fmt::Display::fmt(self.0.as_ref(), formatter)
33    }
34}
35
36impl Error for GraphStorageError {
37    fn source(&self) -> Option<&(dyn Error + 'static)> {
38        Some(self.0.as_ref())
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use crate::{cypher::CypherError, GraphStoreError};
46
47    #[test]
48    fn cloned_graph_errors_retain_the_original_typed_provider_cause() {
49        let error = GraphStoreError::from(StorageBackendError::backend(
50            "fixture",
51            uqa_core::memory::MemoryError::Limit {
52                required: 4096,
53                limit: 32,
54            },
55        ));
56        let retained = error.clone();
57        assert_eq!(retained, error);
58        let original = error.source().unwrap().source().unwrap();
59        let cypher = CypherError::from(retained);
60        let stored = cypher.source().unwrap().source().unwrap();
61        assert!(std::ptr::eq(original, stored));
62        drop(error);
63        let provider = stored.downcast_ref::<StorageBackendError>().unwrap();
64        let memory = provider
65            .source()
66            .unwrap()
67            .downcast_ref::<uqa_core::memory::MemoryError>()
68            .unwrap();
69        assert!(matches!(
70            memory,
71            uqa_core::memory::MemoryError::Limit {
72                required: 4096,
73                limit: 32
74            }
75        ));
76    }
77}