Skip to main content

newsapi_rust/
error.rs

1//! Error types used throughout the library.
2
3use std::time::Duration;
4
5use thiserror::Error;
6
7/// The main error type for the News API client library.
8#[derive(Error, Debug)]
9pub enum NewsApiError {
10    /// An error occurred while making the HTTP request.
11    #[error("HTTP request failed: {0}")]
12    Request(#[from] reqwest::Error),
13
14    /// The API returned an error response.
15    #[error("API returned error: {code} - {message}")]
16    Api {
17        /// The error code returned by the API.
18        code: String,
19        /// The error message returned by the API.
20        message: String,
21    },
22
23    /// The API key provided is invalid.
24    #[error("Invalid API key")]
25    InvalidApiKey,
26
27    /// The API rate limit has been exceeded.
28    #[error("Rate limit exceeded")]
29    RateLimitExceeded,
30
31    /// The requested resource are semantically wrong or missing required parameters.
32    #[error("Invalid Parameters: {0}")]
33    InvalidParams(String),
34
35    /// The request timed out.
36    #[error("Request timed out after {0:?}")]
37    Timeout(Duration),
38
39    /// An error occurred while deserializing the JSON response.
40    #[error("Json deserialization error: {0}")]
41    Json(#[from] serde_json::Error),
42}
43
44/// A specialized Result type for the News API client library.
45pub type Result<T> = std::result::Result<T, NewsApiError>;