kick_api/error.rs
1use thiserror::Error;
2
3/// Errors returned by the Kick API client.
4///
5/// All public methods in this crate return [`Result<T>`](type@Result), which
6/// uses this error type. Match on variants to distinguish network failures,
7/// parse errors, and API-level rejections.
8///
9/// # Example
10///
11/// ```no_run
12/// use kick_api::{KickApiClient, KickApiError};
13///
14/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
15/// let client = KickApiClient::with_token("token".into());
16/// match client.channels().get("xqc").await {
17/// Ok(channel) => println!("{}", channel.slug),
18/// Err(KickApiError::ApiError(msg)) => eprintln!("API error: {msg}"),
19/// Err(KickApiError::HttpRequestError(e)) => eprintln!("Network error: {e}"),
20/// Err(e) => eprintln!("Other error: {e}"),
21/// }
22/// # Ok(())
23/// # }
24/// ```
25#[derive(Error, Debug)]
26pub enum KickApiError {
27 /// An HTTP-level error from `reqwest` (connection refused, timeout, TLS failure, etc.).
28 #[error("HTTP request failed: {0}")]
29 HttpRequestError(#[from] reqwest::Error),
30
31 /// Failed to serialize a request body or deserialize a response.
32 #[error("JSON serialization/deserialization error: {0}")]
33 JsonError(#[from] serde_json::Error),
34
35 /// A parameter passed to a method was invalid (e.g. missing required field).
36 #[error("Invalid input: {0}")]
37 InvalidInput(String),
38
39 /// The Kick API returned a non-success status code or the response body
40 /// indicated an error. Also returned when no OAuth token is set but the
41 /// endpoint requires one.
42 #[error("API returned an error: {0}")]
43 ApiError(String),
44
45 /// A catch-all for errors that don't fit other variants (e.g. `curl` not
46 /// found when calling unofficial API functions).
47 #[error("Unexpected error: {0}")]
48 UnexpectedError(String),
49
50 /// A WebSocket-level error from `tokio-tungstenite` during live chat.
51 #[error("WebSocket error: {0}")]
52 WebSocketError(#[from] tokio_tungstenite::tungstenite::Error),
53}
54
55/// A `Result` type alias that uses [`KickApiError`].
56pub type Result<T> = std::result::Result<T, KickApiError>;