1#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
11#[non_exhaustive]
12pub enum TesseraError {
13 #[error("{0}")]
15 Configuration(String),
16 #[error("{0}")]
18 InvalidArgument(String),
19 #[error("network error contacting Tessera: {0}")]
21 Network(String),
22 #[error("{0}")]
25 UnexpectedResponse(String),
26 #[error(
28 "A presigned download URL was rejected (likely expired). Presigned URLs are short-lived — call read()/scan() again to mint fresh ones."
29 )]
30 PresignExpired,
31 #[error("{message}")]
33 BadRequest {
34 code: Option<String>,
36 message: String,
38 },
39 #[error("{message}")]
41 Authentication {
42 code: Option<String>,
44 message: String,
46 },
47 #[error("{message}")]
49 Forbidden {
50 code: Option<String>,
52 message: String,
54 },
55 #[error("{message}")]
57 NotFound {
58 code: Option<String>,
60 message: String,
62 },
63 #[error("{message}")]
65 ServiceUnavailable {
66 code: Option<String>,
68 message: String,
70 },
71 #[error("{message}")]
73 InternalServer {
74 code: Option<String>,
76 message: String,
78 },
79 #[error("{message}")]
81 Api {
82 status_code: u16,
84 code: Option<String>,
86 message: String,
88 },
89}
90
91impl TesseraError {
92 #[must_use]
94 pub fn status_code(&self) -> Option<u16> {
95 match self {
96 Self::BadRequest { .. } => Some(400),
97 Self::Authentication { .. } => Some(401),
98 Self::Forbidden { .. } => Some(403),
99 Self::NotFound { .. } => Some(404),
100 Self::ServiceUnavailable { .. } => Some(503),
101 Self::InternalServer { .. } => Some(500),
102 Self::Api { status_code, .. } => Some(*status_code),
103 Self::Configuration(_)
104 | Self::InvalidArgument(_)
105 | Self::Network(_)
106 | Self::UnexpectedResponse(_)
107 | Self::PresignExpired => None,
108 }
109 }
110
111 #[must_use]
113 pub fn code(&self) -> Option<&str> {
114 match self {
115 Self::BadRequest { code, .. }
116 | Self::Authentication { code, .. }
117 | Self::Forbidden { code, .. }
118 | Self::NotFound { code, .. }
119 | Self::ServiceUnavailable { code, .. }
120 | Self::InternalServer { code, .. }
121 | Self::Api { code, .. } => code.as_deref(),
122 Self::Configuration(_)
123 | Self::InvalidArgument(_)
124 | Self::Network(_)
125 | Self::UnexpectedResponse(_)
126 | Self::PresignExpired => None,
127 }
128 }
129}
130
131pub fn error_from_response(status_code: u16, body: &[u8]) -> TesseraError {
135 let code: Option<String> = serde_json::from_slice::<serde_json::Value>(body)
136 .ok()
137 .and_then(|parsed| {
138 parsed
139 .get("error")
140 .and_then(serde_json::Value::as_str)
141 .map(ToOwned::to_owned)
142 });
143
144 let detail = code
145 .as_ref()
146 .map_or_else(String::new, |c| format!(" ({c})"));
147 let message = format!("Tessera API request failed with HTTP {status_code}{detail}");
148
149 if let Some(c) = &code {
150 return match c.as_str() {
151 "bad_request" => TesseraError::BadRequest {
152 code: Some(c.clone()),
153 message,
154 },
155 "unauthorized" => TesseraError::Authentication {
156 code: Some(c.clone()),
157 message,
158 },
159 "forbidden" => TesseraError::Forbidden {
160 code: Some(c.clone()),
161 message,
162 },
163 "not_found" => TesseraError::NotFound {
164 code: Some(c.clone()),
165 message,
166 },
167 "unavailable" => TesseraError::ServiceUnavailable {
168 code: Some(c.clone()),
169 message,
170 },
171 "internal" => TesseraError::InternalServer {
172 code: Some(c.clone()),
173 message,
174 },
175 _ => TesseraError::Api {
176 status_code,
177 code: Some(c.clone()),
178 message,
179 },
180 };
181 }
182 match status_code {
183 400 => TesseraError::BadRequest {
184 code: None,
185 message,
186 },
187 401 => TesseraError::Authentication {
188 code: None,
189 message,
190 },
191 403 => TesseraError::Forbidden {
192 code: None,
193 message,
194 },
195 404 => TesseraError::NotFound {
196 code: None,
197 message,
198 },
199 500 => TesseraError::InternalServer {
200 code: None,
201 message,
202 },
203 502..=504 => TesseraError::ServiceUnavailable {
204 code: None,
205 message,
206 },
207 _ => TesseraError::Api {
208 status_code,
209 code: None,
210 message,
211 },
212 }
213}