zello_client/
error.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// SPDX-FileCopyrightText: 2024 John C. Murray
3
4//! Error types for the Zello client
5
6use thiserror::Error;
7
8/// Result type alias for Zello operations
9pub type Result<T> = anyhow::Result<T, ZelloError>;
10
11/// Error types that can occur when using the Zello client
12#[derive(Debug, Error)]
13pub enum ZelloError {
14    /// WebSocket connection error
15    #[error("Connection error: {0}")]
16    ConnectionError(String),
17
18    /// Authentication failed
19    #[error("Authentication error: {0}")]
20    AuthenticationError(String),
21
22    /// Invalid message format or protocol error
23    #[error("Protocol error: {0}")]
24    ProtocolError(String),
25
26    /// Other error
27    #[error("Other error: {0}")]
28    Other(#[from] Box<dyn std::error::Error + Send + Sync>),
29
30    /// Network I/O error
31    #[error("IO error: {0}")]
32    IoError(#[from] std::io::Error),
33
34    /// JSON serialization/deserialization error
35    #[error("JSON error: {0}")]
36    JsonError(#[from] serde_json::Error),
37
38    /// WebSocket error (boxed to reduce size)
39    #[error("WebSocket error: {0}")]
40    WebSocketError(#[from] Box<tokio_tungstenite::tungstenite::Error>),
41
42    /// Audio codec error
43    #[error("Audio error: {0}")]
44    AudioError(String),
45
46    /// Client not connected
47    #[error("Client is not connected")]
48    NotConnected,
49
50    /// Invalid configuration
51    #[error("Configuration error: {0}")]
52    ConfigError(String),
53
54    /// Operation timeout
55    #[error("Operation timed out")]
56    Timeout,
57
58    /// Channel error
59    #[error("Channel error: {0}")]
60    ChannelError(String),
61
62    /// Unknown or unexpected error
63    #[error("Unknown error: {0}")]
64    Unknown(String),
65}
66
67impl From<tokio_tungstenite::tungstenite::Error> for ZelloError {
68    fn from(err: tokio_tungstenite::tungstenite::Error) -> Self {
69        Self::WebSocketError(Box::new(err))
70    }
71}
72
73impl From<Box<dyn std::error::Error>> for ZelloError {
74    fn from(err: Box<dyn std::error::Error>) -> Self {
75        ZelloError::Other(format!("{err}").into())
76    }
77}