1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Error types and helpers for HTTP, transport, hooks, and retries.
//!
//! Most operations return [`crate::Result`]. Use [`Error::status`] and [`Error::body`] on HTTP
//! failures, [`Error::transport_kind`] on transport failures, and [`Error::api_json`] to parse
//! structured API error payloads.
use std::fmt;
use std::sync::Arc;
use bytes::Bytes;
use http::StatusCode;
use thiserror::Error;
/// Classification of underlying transport failures (connection, body, decode, etc.).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TransportKind {
/// Connection failed (TCP/TLS/DNS and similar).
Connect,
/// Request or response body error.
Body,
/// Response body decoding error (e.g. decompression).
Decode,
/// Redirect policy violation.
Redirect,
/// Error building or sending the request.
Request,
/// Invalid request configuration.
Builder,
/// Protocol upgrade failure.
Upgrade,
/// Unclassified transport failure.
Other,
}
impl fmt::Display for TransportKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Connect => write!(f, "connect"),
Self::Body => write!(f, "body"),
Self::Decode => write!(f, "decode"),
Self::Redirect => write!(f, "redirect"),
Self::Request => write!(f, "request"),
Self::Builder => write!(f, "builder"),
Self::Upgrade => write!(f, "upgrade"),
Self::Other => write!(f, "other"),
}
}
}
/// Error type for better-fetch operations.
#[derive(Debug, Error, Clone)]
#[must_use = "errors must be handled or propagated with `?`"]
pub enum Error {
/// Base URL parsing failed ([`ClientBuilder::base_url`](crate::ClientBuilder::base_url)).
#[error("invalid base URL: {0}")]
InvalidBaseUrl(#[from] url::ParseError),
/// Underlying transport failure (connection, DNS, body read, etc.).
#[error("transport error ({kind}): {message}")]
Transport {
/// Coarse category aligned with reqwest's `is_*` helpers.
kind: TransportKind,
/// Human-readable detail (typically from the underlying error's `Display`).
message: String,
/// Underlying error when available (e.g. reqwest).
#[source]
source: Option<Arc<dyn std::error::Error + Send + Sync>>,
},
/// Non-success HTTP response (when using throw mode or `send_json`).
#[error("HTTP {status} {status_text}: {message}")]
Http {
/// HTTP status code.
status: StatusCode,
/// Canonical reason phrase.
status_text: String,
/// Human-readable message.
message: String,
/// Response body when buffered.
body: Option<Bytes>,
},
/// JSON response could not be deserialized (feature `json`).
#[cfg(feature = "json")]
#[error("failed to deserialize response body: {message}")]
Deserialize {
status: StatusCode,
message: String,
body: Option<Bytes>,
},
/// Response failed garde validation (feature `validate`).
#[cfg(feature = "validate")]
#[error("response validation failed: {message}")]
Validation {
status: StatusCode,
message: String,
body: Option<Bytes>,
},
/// Request exceeded the configured timeout.
#[error("request timed out")]
Timeout,
/// Request was cancelled via [`CancellationToken`](crate::CancellationToken).
#[error("request was cancelled")]
Cancelled,
/// Response body exceeded [`ClientBuilder::max_response_bytes`](crate::ClientBuilder::max_response_bytes)
/// or a per-request [`RequestBuilder::max_response_bytes`](crate::RequestBuilder::max_response_bytes) limit.
#[error("response body exceeded limit of {limit} bytes")]
BodyTooLarge {
/// Configured maximum response size in bytes.
limit: u64,
},
/// [`ClientBuilder::build`](crate::ClientBuilder::build) without [`ClientBuilder::base_url`](crate::ClientBuilder::base_url).
#[error("client base URL is required; call ClientBuilder::base_url")]
MissingBaseUrl,
/// Transport retries were exhausted.
#[error("retries exhausted after {attempts} attempts")]
RetryExhausted {
/// Total attempts made (initial + retries).
attempts: u32,
/// Last error before retries were exhausted, when available.
last: Option<Box<Error>>,
},
/// Returned from [`on_request`](crate::hooks::Hooks::on_request) or
/// [`on_response`](crate::hooks::Hooks::on_response) to abort the pipeline.
/// Prefer constructing this with [`Error::hook`](Self::hook) rather than [`Error::Other`](Self::Other).
#[error("hook error: {0}")]
Hook(String),
/// Query parameter serialization failed (typed endpoint query).
///
/// Returned from [`EndpointRequestBuilder::query`](crate::EndpointRequestBuilder::query) and
/// [`EndpointQuery::apply_query`](crate::EndpointQuery::apply_query) when serde serialization fails (since 0.4.0).
#[error("failed to serialize query: {0}")]
QuerySerialize(String),
/// Invalid HTTP header name ([`RequestBuilder::header`](crate::RequestBuilder::header)).
#[error("invalid header name: {0}")]
InvalidHeaderName(String),
/// Invalid HTTP header value ([`RequestBuilder::header`](crate::RequestBuilder::header)).
#[error("invalid header value: {0}")]
InvalidHeaderValue(String),
/// A `:param` segment in the path template was not supplied.
#[error("missing path parameter for `{0}`")]
MissingPathParam(String),
/// Route not registered in a strict [`SchemaRegistry`](crate::SchemaRegistry) (feature `schema`).
#[error("route not in schema registry: {method} {path}")]
SchemaRoute {
/// HTTP method.
method: String,
/// Path template.
path: String,
},
/// JSON Schema validation failed (feature `schema-validate`).
#[cfg(feature = "schema-validate")]
#[error("JSON schema validation failed ({phase}): {message}")]
SchemaValidation {
/// `"request"` or `"response"`.
phase: &'static str,
/// Validator detail.
message: String,
},
/// Invalid `Authorization` header value.
#[error("invalid authorization header: {0}")]
InvalidAuthHeader(String),
/// Request body cannot be replayed for automatic retry (stream or multipart).
#[error("automatic retry is not supported with non-replayable request bodies")]
NonReplayableBody,
/// Request body failed validation before send (feature `validate`).
#[cfg(feature = "validate")]
#[error("request validation failed: {message}")]
RequestValidation {
/// Validation error detail.
message: String,
},
/// I/O error (file writes, etc.).
#[error("I/O error: {0}")]
Io(String),
/// Internal client configuration error.
#[error("configuration error: {0}")]
Config(String),
/// Catch-all for rare plugin errors.
#[error("{0}")]
Other(String),
}
impl Error {
/// Builds a transport error with an explicit [`TransportKind`].
pub fn transport(kind: TransportKind, message: impl Into<String>) -> Self {
Self::Transport {
kind,
message: message.into(),
source: None,
}
}
/// Builds a transport error with an underlying source error.
pub fn transport_with_source(
kind: TransportKind,
message: impl Into<String>,
source: impl std::error::Error + Send + Sync + 'static,
) -> Self {
Self::Transport {
kind,
message: message.into(),
source: Some(Arc::new(source)),
}
}
/// Builds a transport error with [`TransportKind::Other`].
pub fn transport_message(message: impl Into<String>) -> Self {
Self::transport(TransportKind::Other, message)
}
/// Returns the transport error source when present.
pub fn transport_source(&self) -> Option<&(dyn std::error::Error + Send + Sync)> {
match self {
Self::Transport {
source: Some(s), ..
} => Some(s.as_ref()),
_ => None,
}
}
/// Returns the transport failure category when this error is [`Error::Transport`].
pub fn transport_kind(&self) -> Option<TransportKind> {
match self {
Self::Transport { kind, .. } => Some(*kind),
_ => None,
}
}
/// Returns the transport error detail string when this error is [`Error::Transport`].
pub fn transport_detail(&self) -> Option<&str> {
match self {
Self::Transport { message, .. } => Some(message),
_ => None,
}
}
/// Returns `true` when this error is [`Error::Transport`].
pub fn is_transport(&self) -> bool {
matches!(self, Self::Transport { .. })
}
/// Returns `true` when this error is [`Error::Timeout`].
pub fn is_timeout(&self) -> bool {
matches!(self, Self::Timeout)
}
/// Builds an HTTP error with canonical status text.
pub fn http(status: StatusCode, message: impl Into<String>, body: Option<Bytes>) -> Self {
Self::http_with_status_text(
status,
status.canonical_reason().unwrap_or("").to_string(),
message,
body,
)
}
/// Builds an HTTP error with explicit status text.
pub fn http_with_status_text(
status: StatusCode,
status_text: impl Into<String>,
message: impl Into<String>,
body: Option<Bytes>,
) -> Self {
Self::Http {
status,
status_text: status_text.into(),
message: message.into(),
body,
}
}
/// Builds an HTTP error using canonical status text and a message derived from `body` when present.
pub(crate) fn http_error_for_status(status: StatusCode, body: Option<Bytes>) -> Self {
let status_text = status
.canonical_reason()
.unwrap_or("request failed")
.to_string();
let message = body
.as_ref()
.and_then(|b| std::str::from_utf8(b).ok())
.map(|s| s.chars().take(512).collect::<String>())
.filter(|s| !s.is_empty())
.unwrap_or_else(|| status_text.clone());
Self::http_with_status_text(status, status_text, message, body)
}
/// Builds a query serialization error.
pub fn query_serialize(message: impl Into<String>) -> Self {
Self::QuerySerialize(message.into())
}
/// Returns the HTTP status when this error is response-related.
pub fn status(&self) -> Option<StatusCode> {
match self {
Self::Http { status, .. } => Some(*status),
#[cfg(feature = "json")]
Self::Deserialize { status, .. } => Some(*status),
#[cfg(feature = "validate")]
Self::Validation { status, .. } => Some(*status),
_ => None,
}
}
/// Returns the canonical status text for [`Error::Http`].
pub fn status_text(&self) -> Option<&str> {
match self {
Self::Http { status_text, .. } => Some(status_text),
_ => None,
}
}
/// Returns the response body when present on HTTP, deserialize, or validation errors.
pub fn body(&self) -> Option<&Bytes> {
match self {
Self::Http { body, .. } => body.as_ref(),
#[cfg(feature = "json")]
Self::Deserialize { body, .. } => body.as_ref(),
#[cfg(feature = "validate")]
Self::Validation { body, .. } => body.as_ref(),
_ => None,
}
}
/// Returns `true` when transport retries were configured but all attempts failed.
pub fn is_retry_exhausted(&self) -> bool {
matches!(self, Self::RetryExhausted { .. })
}
/// Returns the last error from [`Error::RetryExhausted`] when present.
pub fn retry_exhausted_last(&self) -> Option<&Error> {
match self {
Self::RetryExhausted { last, .. } => last.as_deref(),
_ => None,
}
}
/// Returns `true` when the request was cancelled via [`CancellationToken`](crate::CancellationToken).
pub fn is_cancelled(&self) -> bool {
matches!(self, Self::Cancelled)
}
/// Returns `true` when the response body exceeded a configured size limit.
pub fn is_body_too_large(&self) -> bool {
matches!(self, Self::BodyTooLarge { .. })
}
/// Returns the configured byte limit when this error is [`Error::BodyTooLarge`].
pub fn body_too_large_limit(&self) -> Option<u64> {
match self {
Self::BodyTooLarge { limit } => Some(*limit),
_ => None,
}
}
/// Builds a hook failure for [`Hooks::on_request`](crate::hooks::Hooks::on_request) /
/// [`Hooks::on_response`](crate::hooks::Hooks::on_response).
pub fn hook(msg: impl Into<String>) -> Self {
Self::Hook(msg.into())
}
/// Returns `true` when the error is [`Error::Hook`](Self::Hook).
pub fn is_hook(&self) -> bool {
matches!(self, Self::Hook(_))
}
pub(crate) fn retry_exhausted(attempts: u32, last: Error) -> Self {
Self::RetryExhausted {
attempts,
last: Some(Box::new(last)),
}
}
/// Parses the error response body as JSON (for API error payloads).
///
/// # Examples
///
/// ```
/// use better_fetch::Error;
/// use http::StatusCode;
/// use serde::Deserialize;
///
/// #[derive(Debug, Deserialize, PartialEq)]
/// struct ApiError {
/// message: String,
/// }
///
/// let err = Error::http_with_status_text(
/// StatusCode::BAD_REQUEST,
/// "Bad Request",
/// "bad request",
/// Some(bytes::Bytes::from_static(br#"{"message":"invalid"}"#)),
/// );
/// let api: ApiError = err.api_json().unwrap();
/// assert_eq!(api.message, "invalid");
/// ```
#[cfg(feature = "json")]
pub fn api_json<T: serde::de::DeserializeOwned>(&self) -> Option<T> {
let body = self.body()?;
serde_json::from_slice(body).ok()
}
/// Parses and validates the error response body (feature `validate`).
#[cfg(feature = "validate")]
pub fn api_json_validated<T>(&self) -> Option<T>
where
T: serde::de::DeserializeOwned + garde::Validate,
T::Context: Default,
{
let body = self.body()?;
let value: T = serde_json::from_slice(body).ok()?;
value.validate().ok()?;
Some(value)
}
}
pub(crate) fn map_transport_error(err: reqwest::Error) -> Error {
if err.is_timeout() {
return Error::Timeout;
}
let kind = transport_kind_from_reqwest(&err);
let message = err.to_string();
Error::Transport {
kind,
message,
source: Some(Arc::new(err)),
}
}
fn transport_kind_from_reqwest(err: &reqwest::Error) -> TransportKind {
#[cfg(not(target_arch = "wasm32"))]
if err.is_connect() {
return TransportKind::Connect;
}
if err.is_body() {
TransportKind::Body
} else if err.is_decode() {
TransportKind::Decode
} else if err.is_redirect() {
TransportKind::Redirect
} else if err.is_request() {
TransportKind::Request
} else if err.is_builder() {
TransportKind::Builder
} else if err.is_upgrade() {
TransportKind::Upgrade
} else {
TransportKind::Other
}
}
#[cfg(all(test, feature = "json"))]
mod tests {
use super::*;
use serde::Deserialize;
#[derive(Debug, Deserialize, PartialEq)]
struct ApiError {
message: String,
}
#[test]
fn api_json_parses_http_body() {
let err = Error::http_with_status_text(
StatusCode::BAD_REQUEST,
"Bad Request",
"bad request",
Some(bytes::Bytes::from_static(br#"{"message":"invalid"}"#)),
);
let api: ApiError = err.api_json().unwrap();
assert_eq!(api.message, "invalid");
}
#[test]
fn status_and_status_text_accessors() {
let err = Error::http(StatusCode::NOT_FOUND, "not found", None);
assert_eq!(err.status(), Some(StatusCode::NOT_FOUND));
assert_eq!(err.status_text(), Some("Not Found"));
}
#[test]
fn api_json_returns_none_without_body() {
let err = Error::http(StatusCode::INTERNAL_SERVER_ERROR, "err", None);
assert!(err.api_json::<ApiError>().is_none());
}
#[test]
fn hook_constructor_and_is_hook() {
let err = Error::hook("blocked");
assert!(err.is_hook());
assert!(matches!(err, Error::Hook(msg) if msg == "blocked"));
}
#[test]
fn retry_exhausted_helper_sets_flag() {
let err = Error::retry_exhausted(3, Error::Timeout);
assert!(err.is_retry_exhausted());
assert!(matches!(
err,
Error::RetryExhausted {
attempts: 3,
last: Some(_)
}
));
assert!(matches!(err.retry_exhausted_last(), Some(Error::Timeout)));
}
#[test]
fn transport_helpers() {
let err = Error::transport(TransportKind::Connect, "connection refused");
assert!(err.is_transport());
assert_eq!(err.transport_kind(), Some(TransportKind::Connect));
assert_eq!(err.transport_detail(), Some("connection refused"));
}
#[test]
fn transport_message_defaults_to_other() {
let err = Error::transport_message("tower layer failed");
assert_eq!(err.transport_kind(), Some(TransportKind::Other));
}
}