Skip to main content

a3s_lane/
error.rs

1//! Error types for the lane queue system
2//!
3//! This module defines the error types used throughout the lane queue system.
4//! All errors implement the `std::error::Error` trait via `thiserror::Error`.
5//!
6//! # Error Handling
7//!
8//! The [`LaneError`] enum covers all possible error conditions:
9//! - Lane configuration errors (lane not found, invalid config)
10//! - Queue operation errors (capacity exceeded, shutdown in progress)
11//! - Command execution errors (timeout, execution failure)
12//!
13//! # Example
14//!
15//! ```rust,ignore
16//! use a3s_lane::{QueueManager, LaneError};
17//!
18//! match manager.submit("query", cmd).await {
19//!     Ok(rx) => { /* handle success */ },
20//!     Err(LaneError::LaneNotFound(id)) => {
21//!         eprintln!("Lane '{}' does not exist", id);
22//!     },
23//!     Err(LaneError::ShutdownInProgress) => {
24//!         eprintln!("Queue is shutting down");
25//!     },
26//!     Err(e) => {
27//!         eprintln!("Unexpected error: {}", e);
28//!     }
29//! }
30//! ```
31
32use thiserror::Error;
33
34/// Lane queue error type
35///
36/// Represents all possible errors that can occur in the lane queue system.
37///
38/// # Variants
39///
40/// * `LaneNotFound` - The specified lane ID does not exist in the queue
41/// * `QueueError` - General queue operation error (e.g., capacity exceeded)
42/// * `ConfigError` - Invalid configuration (e.g., min > max concurrency)
43/// * `CommandError` - Command execution failed
44/// * `Timeout` - Command exceeded its timeout duration
45/// * `ShutdownInProgress` - Queue is shutting down and not accepting new commands
46/// * `Other` - Catch-all for unexpected errors
47#[derive(Error, Debug)]
48pub enum LaneError {
49    /// Lane not found
50    #[error("Lane not found: {0}")]
51    LaneNotFound(String),
52
53    /// Queue error
54    #[error("Queue error: {0}")]
55    QueueError(String),
56
57    /// Configuration error
58    #[error("Configuration error: {0}")]
59    ConfigError(String),
60
61    /// Command execution error
62    #[error("Command execution error: {0}")]
63    CommandError(String),
64
65    /// Command timeout
66    #[error("Command timed out after {0:?}")]
67    Timeout(std::time::Duration),
68
69    /// Shutdown in progress
70    #[error("Queue is shutting down, not accepting new commands")]
71    ShutdownInProgress,
72
73    /// Other error
74    #[error("{0}")]
75    Other(String),
76}
77
78/// Result type alias using LaneError
79///
80/// Convenience type alias for `std::result::Result<T, LaneError>`.
81/// Used throughout the library for consistent error handling.
82pub type Result<T> = std::result::Result<T, LaneError>;
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn test_lane_not_found_error() {
90        let error = LaneError::LaneNotFound("query".to_string());
91        assert_eq!(error.to_string(), "Lane not found: query");
92    }
93
94    #[test]
95    fn test_queue_error() {
96        let error = LaneError::QueueError("capacity exceeded".to_string());
97        assert_eq!(error.to_string(), "Queue error: capacity exceeded");
98    }
99
100    #[test]
101    fn test_config_error() {
102        let error = LaneError::ConfigError("invalid concurrency".to_string());
103        assert_eq!(
104            error.to_string(),
105            "Configuration error: invalid concurrency"
106        );
107    }
108
109    #[test]
110    fn test_command_error() {
111        let error = LaneError::CommandError("execution failed".to_string());
112        assert_eq!(
113            error.to_string(),
114            "Command execution error: execution failed"
115        );
116    }
117
118    #[test]
119    fn test_timeout_error() {
120        let error = LaneError::Timeout(std::time::Duration::from_secs(5));
121        assert_eq!(error.to_string(), "Command timed out after 5s");
122    }
123
124    #[test]
125    fn test_shutdown_in_progress_error() {
126        let error = LaneError::ShutdownInProgress;
127        assert_eq!(
128            error.to_string(),
129            "Queue is shutting down, not accepting new commands"
130        );
131    }
132
133    #[test]
134    fn test_other_error() {
135        let error = LaneError::Other("unexpected error".to_string());
136        assert_eq!(error.to_string(), "unexpected error");
137    }
138
139    #[test]
140    fn test_error_debug() {
141        let error = LaneError::LaneNotFound("test".to_string());
142        let debug_str = format!("{:?}", error);
143        assert!(debug_str.contains("LaneNotFound"));
144    }
145}