Skip to main content

type_bridge_server/
error.rs

1#[derive(Debug, thiserror::Error)]
2/// Errors surfaced by configuration, validation, execution, and interception.
3pub enum PipelineError {
4    /// Server configuration could not be loaded or validated.
5    #[error("Configuration error: {0}")]
6    Config(String),
7    /// The configured TypeDB endpoint could not be reached.
8    #[error("TypeDB connection error: {0}")]
9    Connection(String),
10    /// The connected TypeDB server version is outside the supported matrix.
11    #[error("Unsupported version: {0}")]
12    UnsupportedVersion(#[from] type_bridge_core_lib::version::VersionError),
13    /// Provider execution rejected or failed the query.
14    #[error("Query execution error: {0}")]
15    QueryExecution(String),
16    /// Static query validation failed.
17    #[error("Validation error: {0}")]
18    Validation(String),
19    /// Query text or wire input could not be parsed.
20    #[error("Parse error: {0}")]
21    Parse(String),
22    /// The configured schema could not be loaded or decoded.
23    #[error("Schema error: {0}")]
24    Schema(String),
25    /// An interceptor rejected or failed the request.
26    #[error("Interceptor error: {0}")]
27    Interceptor(String),
28    /// An unexpected internal invariant failed.
29    #[error("Internal error: {0}")]
30    Internal(String),
31}
32
33impl PipelineError {
34    /// Typed provider failure from runtime-owned redacted descriptions.
35    /// Released V1 carriers have no staged operation index or schema context.
36    #[must_use]
37    pub fn failure(&self) -> Option<type_bridge_contract::failure::Failure> {
38        #[cfg(feature = "typedb")]
39        match self {
40            Self::QueryExecution(message) => {
41                type_bridge_typedb_runtime::failure::failure_from_message(message)
42            }
43            _ => None,
44        }
45        #[cfg(not(feature = "typedb"))]
46        {
47            None
48        }
49    }
50}
51
52#[cfg(test)]
53#[cfg_attr(coverage_nightly, coverage(off))]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn display_config_error() {
59        let e = PipelineError::Config("bad config".into());
60        assert_eq!(e.to_string(), "Configuration error: bad config");
61    }
62
63    #[test]
64    fn display_connection_error() {
65        let e = PipelineError::Connection("refused".into());
66        assert_eq!(e.to_string(), "TypeDB connection error: refused");
67    }
68
69    #[test]
70    fn display_query_execution_error() {
71        let e = PipelineError::QueryExecution("syntax error".into());
72        assert_eq!(e.to_string(), "Query execution error: syntax error");
73    }
74
75    #[test]
76    fn display_validation_error() {
77        let e = PipelineError::Validation("invalid type".into());
78        assert_eq!(e.to_string(), "Validation error: invalid type");
79    }
80
81    #[test]
82    fn display_parse_error() {
83        let e = PipelineError::Parse("unexpected token".into());
84        assert_eq!(e.to_string(), "Parse error: unexpected token");
85    }
86
87    #[test]
88    fn display_schema_error() {
89        let e = PipelineError::Schema("file not found".into());
90        assert_eq!(e.to_string(), "Schema error: file not found");
91    }
92
93    #[test]
94    fn display_interceptor_error() {
95        let e = PipelineError::Interceptor("access denied".into());
96        assert_eq!(e.to_string(), "Interceptor error: access denied");
97    }
98
99    #[test]
100    fn display_internal_error() {
101        let e = PipelineError::Internal("unexpected".into());
102        assert_eq!(e.to_string(), "Internal error: unexpected");
103    }
104
105    #[test]
106    fn debug_format() {
107        let e = PipelineError::Config("test".into());
108        let debug = format!("{:?}", e);
109        assert!(debug.contains("Config"));
110    }
111
112    #[test]
113    fn display_unsupported_version_error() {
114        use type_bridge_core_lib::version::{Version, VersionError};
115        let inner = VersionError::Unsupported {
116            component: "server",
117            found: Version::new(2, 28, 0),
118        };
119        let e = PipelineError::UnsupportedVersion(inner);
120        let msg = e.to_string();
121        assert!(msg.contains("Unsupported version"), "missing prefix: {msg}");
122        assert!(msg.contains("2.28.0"), "missing detected version: {msg}");
123    }
124}