Skip to main content

nautilus_core/
error.rs

1//! Error types for Nautilus core.
2
3/// Error type for Nautilus core.
4///
5/// These variants cover query-building and type-conversion failures only.
6/// Runtime execution errors (database, connection, row decoding) live in
7/// `nautilus-connector::ConnectorError`.
8#[derive(Debug, Clone, PartialEq, thiserror::Error)]
9pub enum Error {
10    /// Invalid query construction.
11    #[error("Invalid query: {0}")]
12    InvalidQuery(String),
13    /// Missing required field.
14    #[error("Missing required field: {0}")]
15    MissingField(String),
16    /// Type conversion error.
17    #[error("Type error: {0}")]
18    TypeError(String),
19    /// Record not found (used by `*_or_throw` operations).
20    #[error("Record not found: {0}")]
21    NotFound(String),
22    /// Generic error.
23    #[error("{0}")]
24    Other(String),
25}
26
27/// Result type alias.
28pub type Result<T> = std::result::Result<T, Error>;
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn test_error_display() {
36        let err = Error::InvalidQuery("test".to_string());
37        assert_eq!(err.to_string(), "Invalid query: test");
38
39        let err = Error::MissingField("name".to_string());
40        assert_eq!(err.to_string(), "Missing required field: name");
41    }
42
43    #[test]
44    fn test_result_type() {
45        let ok: Result<i32> = Ok(42);
46        assert!(ok.is_ok());
47
48        let err: Result<i32> = Err(Error::Other("failed".to_string()));
49        assert!(err.is_err());
50    }
51
52    #[test]
53    fn test_not_found_and_type_error() {
54        let err = Error::NotFound("user with id=99".to_string());
55        assert_eq!(err.to_string(), "Record not found: user with id=99");
56
57        let err = Error::TypeError("expected i64, got Bool".to_string());
58        assert_eq!(err.to_string(), "Type error: expected i64, got Bool");
59
60        let err = Error::Other("some failure".to_string());
61        assert_eq!(err.to_string(), "some failure");
62
63        let err = Error::MissingField("email".to_string());
64        assert_eq!(err.to_string(), "Missing required field: email");
65    }
66}