Skip to main content

mimo_api/
error.rs

1//! Error types for the MiMo API client.
2
3use thiserror::Error;
4
5/// Result type alias for MiMo API operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Error types that can occur when using the MiMo API.
9#[derive(Error, Debug)]
10pub enum Error {
11    /// API key is missing from environment variables.
12    #[error("API key not found. Please set the XIAOMI_API_KEY environment variable.")]
13    MissingApiKey,
14
15    /// HTTP request failed.
16    #[error("HTTP request failed: {0}")]
17    HttpError(#[from] reqwest::Error),
18
19    /// JSON serialization/deserialization failed.
20    #[error("JSON error: {0}")]
21    JsonError(#[from] serde_json::Error),
22
23    /// API returned an error response.
24    #[error("API error: {message} (status: {status})")]
25    ApiError {
26        /// HTTP status code
27        status: u16,
28        /// Error message from the API
29        message: String,
30    },
31
32    /// Stream parsing error.
33    #[error("Stream parsing error: {0}")]
34    StreamError(String),
35
36    /// Invalid parameter value.
37    #[error("Invalid parameter: {0}")]
38    InvalidParameter(String),
39
40    /// Invalid response from API.
41    #[error("Invalid response: {0}")]
42    InvalidResponse(String),
43
44    /// IO error.
45    #[error("IO error: {0}")]
46    IoError(#[from] std::io::Error),
47}
48
49impl Error {
50    /// Create a new API error.
51    pub fn api_error(status: u16, message: impl Into<String>) -> Self {
52        Error::ApiError {
53            status,
54            message: message.into(),
55        }
56    }
57}