Skip to main content

oxigdal_stac/
error.rs

1//! Error types for STAC operations.
2
3/// Result type for STAC operations.
4pub type Result<T> = core::result::Result<T, StacError>;
5
6/// Error types that can occur during STAC operations.
7#[derive(Debug, thiserror::Error)]
8pub enum StacError {
9    /// Invalid STAC type (not a Catalog, Collection, or Item).
10    #[error("Invalid STAC type: expected {expected}, found {found}")]
11    InvalidType {
12        /// Expected STAC type.
13        expected: String,
14        /// Found STAC type.
15        found: String,
16    },
17
18    /// Invalid STAC version.
19    #[error("Invalid STAC version: {0}")]
20    InvalidVersion(String),
21
22    /// Missing required field.
23    #[error("Missing required field: {0}")]
24    MissingField(String),
25
26    /// Invalid field value.
27    #[error("Invalid field value for {field}: {reason}")]
28    InvalidFieldValue {
29        /// Field name.
30        field: String,
31        /// Reason for invalidity.
32        reason: String,
33    },
34
35    /// Invalid URL.
36    #[error("Invalid URL: {0}")]
37    InvalidUrl(String),
38
39    /// Invalid geometry.
40    #[error("Invalid geometry: {0}")]
41    InvalidGeometry(String),
42
43    /// Invalid datetime.
44    #[error("Invalid datetime: {0}")]
45    InvalidDatetime(String),
46
47    /// Invalid bbox.
48    #[error("Invalid bbox: {0}")]
49    InvalidBbox(String),
50
51    /// Serialization error.
52    #[error("Serialization error: {0}")]
53    Serialization(String),
54
55    /// Deserialization error.
56    #[error("Deserialization error: {0}")]
57    Deserialization(String),
58
59    /// HTTP request error.
60    #[cfg(feature = "reqwest")]
61    #[error("HTTP request error: {0}")]
62    Http(String),
63
64    /// Asset not found.
65    #[error("Asset not found: {0}")]
66    AssetNotFound(String),
67
68    /// Link not found.
69    #[error("Link not found: {0}")]
70    LinkNotFound(String),
71
72    /// Extension not found.
73    #[error("Extension not found: {0}")]
74    ExtensionNotFound(String),
75
76    /// Invalid extension data.
77    #[error("Invalid extension data for {extension}: {reason}")]
78    InvalidExtension {
79        /// Extension identifier.
80        extension: String,
81        /// Reason for invalidity.
82        reason: String,
83    },
84
85    /// IO error.
86    #[error("IO error: {0}")]
87    Io(String),
88
89    /// Invalid search parameters.
90    #[error("Invalid search parameters: {0}")]
91    InvalidSearchParams(String),
92
93    /// API response error.
94    #[error("API response error: {0}")]
95    ApiResponse(String),
96
97    /// Builder error.
98    #[error("Builder error: {0}")]
99    Builder(String),
100
101    /// Item already exists (transaction conflict).
102    #[error("Item already exists: {0}")]
103    AlreadyExists(String),
104
105    /// Item or resource not found.
106    #[error("Not found: {0}")]
107    NotFound(String),
108
109    /// Invalid item data.
110    #[error("Invalid item: {0}")]
111    InvalidItem(String),
112
113    /// Other error.
114    #[error("{0}")]
115    Other(String),
116}
117
118impl From<serde_json::Error> for StacError {
119    fn from(err: serde_json::Error) -> Self {
120        if err.is_data() {
121            StacError::Deserialization(err.to_string())
122        } else {
123            StacError::Serialization(err.to_string())
124        }
125    }
126}
127
128impl From<url::ParseError> for StacError {
129    fn from(err: url::ParseError) -> Self {
130        StacError::InvalidUrl(err.to_string())
131    }
132}
133
134impl From<chrono::ParseError> for StacError {
135    fn from(err: chrono::ParseError) -> Self {
136        StacError::InvalidDatetime(err.to_string())
137    }
138}
139
140#[cfg(feature = "reqwest")]
141impl From<reqwest::Error> for StacError {
142    fn from(err: reqwest::Error) -> Self {
143        StacError::Http(err.to_string())
144    }
145}
146
147impl From<geojson::Error> for StacError {
148    fn from(err: geojson::Error) -> Self {
149        StacError::InvalidGeometry(err.to_string())
150    }
151}
152
153#[cfg(feature = "std")]
154impl From<std::io::Error> for StacError {
155    fn from(err: std::io::Error) -> Self {
156        StacError::Io(err.to_string())
157    }
158}