Skip to main content

types_sdk/
error.rs

1//! Public error types for the `types` module.
2//!
3//! These errors are safe to expose to other modules and consumers.
4
5use thiserror::Error;
6
7/// Errors that can be returned by the `TypesClient`.
8#[derive(Error, Debug, Clone)]
9pub enum TypesError {
10    /// Core types are not yet registered.
11    #[error("Core types not ready")]
12    NotReady,
13
14    /// Failed to register core types.
15    #[error("Registration failed: {0}")]
16    RegistrationFailed(String),
17
18    /// An internal error occurred.
19    #[error("Internal error: {0}")]
20    Internal(String),
21}
22
23impl TypesError {
24    /// Creates a `NotReady` error.
25    #[must_use]
26    pub const fn not_ready() -> Self {
27        Self::NotReady
28    }
29
30    /// Creates a `RegistrationFailed` error.
31    #[must_use]
32    pub fn registration_failed(message: impl Into<String>) -> Self {
33        Self::RegistrationFailed(message.into())
34    }
35
36    /// Creates an `Internal` error.
37    #[must_use]
38    pub fn internal(message: impl Into<String>) -> Self {
39        Self::Internal(message.into())
40    }
41
42    /// Returns `true` if this is a not ready error.
43    #[must_use]
44    pub const fn is_not_ready(&self) -> bool {
45        matches!(self, Self::NotReady)
46    }
47
48    /// Returns `true` if this is a registration failed error.
49    #[must_use]
50    pub const fn is_registration_failed(&self) -> bool {
51        matches!(self, Self::RegistrationFailed(_))
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_error_constructors() {
61        let err = TypesError::not_ready();
62        assert!(err.is_not_ready());
63
64        let err = TypesError::registration_failed("schema invalid");
65        assert!(err.is_registration_failed());
66        assert!(err.to_string().contains("schema invalid"));
67
68        let err = TypesError::internal("unexpected");
69        assert!(matches!(err, TypesError::Internal(_)));
70    }
71
72    #[test]
73    fn test_error_display() {
74        let err = TypesError::NotReady;
75        assert_eq!(err.to_string(), "Core types not ready");
76
77        let err = TypesError::RegistrationFailed("failed to parse".to_owned());
78        assert_eq!(err.to_string(), "Registration failed: failed to parse");
79
80        let err = TypesError::Internal("database error".to_owned());
81        assert_eq!(err.to_string(), "Internal error: database error");
82    }
83}