Skip to main content

rullst_orm/polyglot/
error.rs

1use std::fmt;
2
3/// Errors returned by the explicit polyglot persistence boundary.
4#[derive(Debug, thiserror::Error)]
5#[non_exhaustive]
6pub enum PolyglotError {
7    /// A collection, database, namespace, or document identifier was rejected.
8    #[error("invalid {kind}: {reason}")]
9    InvalidIdentifier {
10        /// Identifier category.
11        kind: &'static str,
12        /// Stable, credential-free failure detail.
13        reason: &'static str,
14    },
15    /// An adapter configuration is unsafe or incomplete.
16    #[error("invalid {backend} configuration: {reason}")]
17    InvalidConfiguration {
18        /// Backend being configured.
19        backend: &'static str,
20        /// Stable, credential-free failure detail.
21        reason: &'static str,
22    },
23    /// A record with the requested identifier already exists.
24    #[error("document already exists")]
25    Conflict,
26    /// A record required by a mutating operation does not exist.
27    #[error("document not found")]
28    NotFound,
29    /// A model could not be serialized or decoded.
30    #[error("serialization failed: {0}")]
31    Serialization(String),
32    /// A backend driver returned an error.
33    #[error("{backend} operation failed: {message}")]
34    Driver {
35        /// Backend that returned the error.
36        backend: &'static str,
37        /// Error text with secrets excluded by the adapter.
38        message: String,
39    },
40    /// A response exceeded the adapter's configured memory bound.
41    #[error("{backend} response exceeded the {limit_bytes}-byte limit")]
42    ResponseTooLarge {
43        /// Backend that returned the response.
44        backend: &'static str,
45        /// Maximum accepted response size.
46        limit_bytes: usize,
47    },
48    /// The backend cannot represent a value through the portable API.
49    #[error("{backend} value type is unsupported: {kind}")]
50    UnsupportedValue {
51        /// Backend that produced the value.
52        backend: &'static str,
53        /// Backend type name.
54        kind: String,
55    },
56    /// Work delegated to a blocking worker could not complete.
57    #[error("{backend} worker failed: {message}")]
58    Worker {
59        /// Backend whose worker stopped.
60        backend: &'static str,
61        /// Join or lock failure detail.
62        message: String,
63    },
64}
65
66impl PolyglotError {
67    #[cfg(any(
68        feature = "duckdb",
69        feature = "mongodb",
70        feature = "surrealdb",
71        feature = "turso"
72    ))]
73    pub(crate) fn driver(backend: &'static str, error: impl fmt::Display) -> Self {
74        Self::Driver {
75            backend,
76            message: error.to_string(),
77        }
78    }
79
80    pub(crate) fn serialization(error: impl fmt::Display) -> Self {
81        Self::Serialization(error.to_string())
82    }
83
84    /// Converts a redacted model-codec failure into the public error contract.
85    #[doc(hidden)]
86    pub fn serialization_public(error: impl fmt::Display) -> Self {
87        Self::serialization(error)
88    }
89}