stac_client/
error.rs

1//! Defines the error types and `Result` alias used throughout the `stac-client` crate.
2//!
3//! This module centralizes all error handling, providing a unified `Error` enum
4//! that covers HTTP transport issues, JSON parsing failures, and STAC API-specific
5//! error conditions.
6
7use thiserror::Error;
8
9/// Convenience result type used throughout the crate.
10pub type Result<T> = std::result::Result<T, Error>;
11
12/// Crate-wide error enumeration covering transport, parsing, and domain errors.
13#[derive(Error, Debug)]
14pub enum Error {
15    /// An HTTP transport or protocol level error.
16    #[error("HTTP request error: {0}")]
17    Http(#[from] reqwest::Error),
18
19    /// JSON (de)serialization error.
20    #[error("JSON serialization/deserialization error: {0}")]
21    Json(#[from] serde_json::Error),
22
23    /// URL parsing failure while constructing endpoint URLs.
24    #[error("URL parsing error: {0}")]
25    Url(#[from] url::ParseError),
26
27    /// The provided STAC API endpoint URL cannot be modified or is invalid for request building.
28    #[error("Invalid STAC API endpoint: {0}")]
29    InvalidEndpoint(String),
30
31    /// A non-success HTTP status code returned by the STAC API.
32    #[error("STAC API error: {status} - {message}")]
33    Api {
34        /// HTTP status code returned by the server.
35        status: u16,
36        /// Body text (or synthesized message) returned by the server.
37        message: String,
38    },
39    /// The client has been rate limited (HTTP 429). Contains optional retry-after seconds.
40    #[error("Rate limited: retry after {retry_after:?} seconds")]
41    RateLimited {
42        /// Suggested wait duration in seconds (if provided by server via Retry-After header).
43        retry_after: Option<u64>,
44    },
45
46    /// A requested item could not be located.
47    #[error("Item not found: {0}")]
48    ItemNotFound(String),
49
50    /// A requested collection could not be located.
51    #[error("Collection not found: {0}")]
52    CollectionNotFound(String),
53
54    /// Provided search parameters are invalid or inconsistent.
55    #[error("Invalid search parameters: {0}")]
56    InvalidSearchParams(String),
57
58    /// An I/O error occurred, e.g., while saving a downloaded asset.
59    #[error("I/O error: {0}")]
60    Io(#[from] std::io::Error),
61}