Skip to main content

librus_rs/
error.rs

1//! Error types for the Librus API client.
2
3use thiserror::Error;
4
5/// Errors that can occur when using the Librus API client.
6///
7/// # Example
8///
9/// ```rust,no_run
10/// use librus_rs::{Client, Error};
11///
12/// # async fn example() {
13/// match Client::from_env().await {
14///     Ok(_) => println!("Success"),
15///     Err(Error::MissingEnvVar(var)) => eprintln!("Missing {}", var),
16///     Err(Error::Authentication) => eprintln!("Bad credentials"),
17///     Err(e) => eprintln!("Error: {}", e),
18/// }
19/// # }
20/// ```
21#[derive(Debug, Error)]
22pub enum Error {
23    /// Authentication with Librus failed due to invalid credentials or server error.
24    #[error("authentication failed: invalid credentials or server error")]
25    Authentication,
26
27    /// Required environment variable is not set.
28    ///
29    /// Returned by [`Client::from_env()`](crate::Client::from_env) when
30    /// `LIBRUS_USERNAME` or `LIBRUS_PASSWORD` is missing.
31    #[error("environment variable `{0}` is not set")]
32    MissingEnvVar(&'static str),
33
34    /// Required credential is missing from the builder.
35    ///
36    /// Returned by [`ClientBuilder::build()`](crate::ClientBuilder::build) when
37    /// username or password was not provided.
38    #[error("missing required credential: {0}")]
39    MissingCredentials(&'static str),
40
41    /// HTTP client construction failed.
42    #[error("failed to build HTTP client: {0}")]
43    HttpClient(#[source] reqwest::Error),
44
45    /// HTTP request failed due to network or connection error.
46    #[error("request failed: {0}")]
47    Request(#[source] reqwest::Error),
48
49    /// API returned an error response.
50    ///
51    /// Contains the HTTP status code and response body for debugging.
52    #[error("API error (status {status}): {body}")]
53    ApiError {
54        /// HTTP status code returned by the API.
55        status: u16,
56        /// Response body content.
57        body: String,
58    },
59
60    /// Failed to parse API response as JSON.
61    ///
62    /// This usually indicates an unexpected response format from the API.
63    #[error("failed to parse response: {source}")]
64    Parse {
65        /// The underlying JSON parsing error.
66        #[source]
67        source: serde_json::Error,
68        /// The raw response body that failed to parse.
69        body: String,
70    },
71}