Skip to main content

sonos_state/
error.rs

1//! Error types for sonos-state
2
3/// Result type for sonos-state operations
4pub type Result<T> = std::result::Result<T, StateError>;
5
6/// Errors that can occur during state management
7#[derive(Debug, thiserror::Error)]
8pub enum StateError {
9    /// Error during initialization
10    #[error("Initialization error: {0}")]
11    Init(String),
12
13    /// Error parsing data
14    #[error("Parse error: {0}")]
15    Parse(String),
16
17    /// Error from sonos-api
18    #[error("API error: {0}")]
19    Api(#[from] sonos_api::ApiError),
20
21    /// State manager is already running
22    #[error("State manager is already running")]
23    AlreadyRunning,
24
25    /// Shutdown failed
26    #[error("Shutdown failed")]
27    ShutdownFailed,
28
29    /// Lock acquisition failed
30    #[error("Lock error: {0}")]
31    LockError(String),
32
33    /// Speaker not found
34    #[error("Speaker not found: {0:?}")]
35    SpeakerNotFound(crate::model::SpeakerId),
36
37    /// Invalid URL
38    #[error("Invalid URL: {0}")]
39    InvalidUrl(String),
40
41    /// Initialization failed
42    #[error("Initialization failed: {0}")]
43    InitializationFailed(String),
44
45    /// Device registration failed
46    #[error("Device registration failed: {0}")]
47    DeviceRegistrationFailed(String),
48
49    /// Subscription failed
50    #[error("Subscription failed: {0}")]
51    SubscriptionFailed(String),
52
53    /// Invalid IP address
54    #[error("Invalid IP address: {0}")]
55    InvalidIpAddress(String),
56
57    /// Lock poisoned (internal mutex error)
58    #[error("Internal lock poisoned")]
59    LockPoisoned,
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65    use std::error::Error as _;
66
67    #[test]
68    fn api_variant_keeps_message_and_source() {
69        let err = StateError::from(sonos_api::ApiError::NetworkError("boom".to_string()));
70        assert_eq!(err.to_string(), "API error: Network error: boom");
71        assert!(err.source().is_some());
72    }
73}