http-cache 1.0.0-alpha.6

An HTTP caching middleware
Documentation
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
543
544
545
546
547
use std::fmt;

/// Generic error type for the `HttpCache` middleware.
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;

/// A `Result` typedef to use with the [`BoxError`] type
pub type Result<T> = std::result::Result<T, BoxError>;

/// Error type for unknown http versions
#[derive(Debug, Default, Copy, Clone)]
pub struct BadVersion;

impl fmt::Display for BadVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.pad("Unknown HTTP version")
    }
}

impl std::error::Error for BadVersion {}

/// Error type for bad header values
#[derive(Debug, Default, Copy, Clone)]
pub struct BadHeader;

impl fmt::Display for BadHeader {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.pad("Error parsing header value")
    }
}

impl std::error::Error for BadHeader {}

/// Error type for request parsing failure
#[derive(Debug, Default, Copy, Clone)]
pub struct BadRequest;

impl fmt::Display for BadRequest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.pad("Request object is not cloneable. Are you passing a streaming body?")
    }
}

impl std::error::Error for BadRequest {}

/// Unified error type for HTTP cache operations that works across all client libraries.
///
/// This enum consolidates error handling patterns from all http-cache client crates
/// (reqwest, surf, tower, ureq) while providing a clean, extensible interface.
///
/// # Examples
///
/// ```rust
/// use http_cache::{HttpCacheError, BadRequest};
///
/// // Cache operation errors
/// let cache_err = HttpCacheError::cache("Failed to read cache entry");
///
/// // Request parsing errors
/// let request_err = HttpCacheError::from(BadRequest);
///
/// // HTTP processing errors
/// let http_err = HttpCacheError::http("Invalid header format");
///
/// // Body processing errors  
/// let body_err = HttpCacheError::body("Failed to collect request body");
/// ```
#[derive(Debug)]
pub enum HttpCacheError {
    /// HTTP client error (reqwest, surf, etc.)
    Client(BoxError),
    /// HTTP cache operation failed
    Cache(String),
    /// Request parsing failed (e.g., non-cloneable request)
    BadRequest(BadRequest),
    /// HTTP processing error (header parsing, version handling, etc.)
    Http(BoxError),
    /// Body processing error (collection, streaming, etc.)
    Body(BoxError),
    /// Streaming operation error (with detailed error kind)
    Streaming(StreamingError),
    /// Other generic error
    Other(BoxError),
}

impl HttpCacheError {
    /// Create a cache operation error
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_cache::HttpCacheError;
    ///
    /// let err = HttpCacheError::cache("Cache entry not found");
    /// ```
    pub fn cache<S: Into<String>>(message: S) -> Self {
        Self::Cache(message.into())
    }

    /// Create an HTTP processing error
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_cache::HttpCacheError;
    ///
    /// let err = HttpCacheError::http("Invalid header format");
    /// ```
    pub fn http<E: Into<BoxError>>(error: E) -> Self {
        Self::Http(error.into())
    }

    /// Create a body processing error
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_cache::HttpCacheError;
    ///
    /// let err = HttpCacheError::body("Failed to collect request body");
    /// ```
    pub fn body<E: Into<BoxError>>(error: E) -> Self {
        Self::Body(error.into())
    }

    /// Create a client error
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_cache::HttpCacheError;
    ///
    /// let err = HttpCacheError::client("Network timeout");
    /// ```
    pub fn client<E: Into<BoxError>>(error: E) -> Self {
        Self::Client(error.into())
    }

    /// Create a generic error
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_cache::HttpCacheError;
    ///
    /// let err = HttpCacheError::other("Unexpected error occurred");
    /// ```
    pub fn other<E: Into<BoxError>>(error: E) -> Self {
        Self::Other(error.into())
    }

    /// Returns true if this error is related to cache operations
    pub fn is_cache_error(&self) -> bool {
        matches!(self, Self::Cache(_))
    }

    /// Returns true if this error is related to client operations
    pub fn is_client_error(&self) -> bool {
        matches!(self, Self::Client(_))
    }

    /// Returns true if this error is related to streaming operations
    pub fn is_streaming_error(&self) -> bool {
        matches!(self, Self::Streaming(_))
    }

    /// Returns true if this error is a bad request
    pub fn is_bad_request(&self) -> bool {
        matches!(self, Self::BadRequest(_))
    }
}

impl fmt::Display for HttpCacheError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Client(e) => write!(f, "HTTP client error: {e}"),
            Self::Cache(msg) => write!(f, "Cache error: {msg}"),
            Self::BadRequest(e) => write!(f, "Request error: {e}"),
            Self::Http(e) => write!(f, "HTTP error: {e}"),
            Self::Body(e) => write!(f, "Body processing error: {e}"),
            Self::Streaming(e) => write!(f, "Streaming error: {e}"),
            Self::Other(e) => write!(f, "Other error: {e}"),
        }
    }
}

impl std::error::Error for HttpCacheError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Client(e) => Some(e.as_ref()),
            Self::Cache(_) => None,
            Self::BadRequest(e) => Some(e),
            Self::Http(e) => Some(e.as_ref()),
            Self::Body(e) => Some(e.as_ref()),
            Self::Streaming(e) => Some(e),
            Self::Other(e) => Some(e.as_ref()),
        }
    }
}

// Comprehensive From implementations for common error types

impl From<BadRequest> for HttpCacheError {
    fn from(error: BadRequest) -> Self {
        Self::BadRequest(error)
    }
}

impl From<BadHeader> for HttpCacheError {
    fn from(error: BadHeader) -> Self {
        Self::Http(Box::new(error))
    }
}

impl From<BadVersion> for HttpCacheError {
    fn from(error: BadVersion) -> Self {
        Self::Http(Box::new(error))
    }
}

impl From<StreamingError> for HttpCacheError {
    fn from(error: StreamingError) -> Self {
        Self::Streaming(error)
    }
}

impl From<BoxError> for HttpCacheError {
    fn from(error: BoxError) -> Self {
        Self::Other(error)
    }
}

impl From<std::io::Error> for HttpCacheError {
    fn from(error: std::io::Error) -> Self {
        Self::Other(Box::new(error))
    }
}

impl From<http::Error> for HttpCacheError {
    fn from(error: http::Error) -> Self {
        Self::Http(Box::new(error))
    }
}

impl From<http::header::InvalidHeaderValue> for HttpCacheError {
    fn from(error: http::header::InvalidHeaderValue) -> Self {
        Self::Http(Box::new(error))
    }
}

impl From<http::header::InvalidHeaderName> for HttpCacheError {
    fn from(error: http::header::InvalidHeaderName) -> Self {
        Self::Http(Box::new(error))
    }
}

impl From<http::uri::InvalidUri> for HttpCacheError {
    fn from(error: http::uri::InvalidUri) -> Self {
        Self::Http(Box::new(error))
    }
}

impl From<http::method::InvalidMethod> for HttpCacheError {
    fn from(error: http::method::InvalidMethod) -> Self {
        Self::Http(Box::new(error))
    }
}

impl From<http::status::InvalidStatusCode> for HttpCacheError {
    fn from(error: http::status::InvalidStatusCode) -> Self {
        Self::Http(Box::new(error))
    }
}

#[cfg(feature = "url-standard")]
impl From<url::ParseError> for HttpCacheError {
    fn from(error: url::ParseError) -> Self {
        Self::Http(Box::new(error))
    }
}

// Note: Client-specific error conversions (reqwest, surf, ureq, etc.)
// are implemented in their respective http-cache-* crates to avoid
// feature dependencies in the core http-cache crate.

// Type alias for results using the unified error type
/// A `Result` type alias for HTTP cache operations using [`HttpCacheError`]
pub type HttpCacheResult<T> = std::result::Result<T, HttpCacheError>;

/// Error type for streaming operations
#[derive(Debug)]
pub struct StreamingError {
    inner: BoxError,
    kind: StreamingErrorKind,
}

/// Different kinds of streaming errors for better error handling
#[derive(Debug, Clone, Copy)]
pub enum StreamingErrorKind {
    /// I/O error (file operations, network)
    Io,
    /// Serialization/deserialization error
    Serialization,
    /// Lock contention or synchronization error
    Concurrency,
    /// Cache consistency error
    Consistency,
    /// Temporary file management error
    TempFile,
    /// Content addressing error (SHA256, file paths)
    ContentAddressing,
    /// Client library error (e.g., reqwest, surf)
    Client,
    /// Generic streaming error
    Other,
}

impl StreamingError {
    /// Create a new streaming error from any error type
    pub fn new<E: Into<BoxError>>(error: E) -> Self {
        Self { inner: error.into(), kind: StreamingErrorKind::Other }
    }

    /// Create a streaming error with a specific kind
    pub fn with_kind<E: Into<BoxError>>(
        error: E,
        kind: StreamingErrorKind,
    ) -> Self {
        Self { inner: error.into(), kind }
    }

    /// Create an I/O error
    pub fn io<E: Into<BoxError>>(error: E) -> Self {
        Self::with_kind(error, StreamingErrorKind::Io)
    }

    /// Create a serialization error
    pub fn serialization<E: Into<BoxError>>(error: E) -> Self {
        Self::with_kind(error, StreamingErrorKind::Serialization)
    }

    /// Create a concurrency error
    pub fn concurrency<E: Into<BoxError>>(error: E) -> Self {
        Self::with_kind(error, StreamingErrorKind::Concurrency)
    }

    /// Create a consistency error
    pub fn consistency<E: Into<BoxError>>(error: E) -> Self {
        Self::with_kind(error, StreamingErrorKind::Consistency)
    }

    /// Create a temp file error
    pub fn temp_file<E: Into<BoxError>>(error: E) -> Self {
        Self::with_kind(error, StreamingErrorKind::TempFile)
    }

    /// Create a content addressing error
    pub fn content_addressing<E: Into<BoxError>>(error: E) -> Self {
        Self::with_kind(error, StreamingErrorKind::ContentAddressing)
    }

    /// Create a client error
    pub fn client<E: Into<BoxError>>(error: E) -> Self {
        Self::with_kind(error, StreamingErrorKind::Client)
    }

    /// Get the error kind
    pub fn kind(&self) -> &StreamingErrorKind {
        &self.kind
    }
}

impl fmt::Display for StreamingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Streaming error: {}", self.inner)
    }
}

impl std::error::Error for StreamingError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&*self.inner)
    }
}

impl From<BoxError> for StreamingError {
    fn from(error: BoxError) -> Self {
        Self::new(error)
    }
}

impl From<std::convert::Infallible> for StreamingError {
    fn from(never: std::convert::Infallible) -> Self {
        match never {}
    }
}

impl From<std::io::Error> for StreamingError {
    fn from(error: std::io::Error) -> Self {
        Self::new(error)
    }
}

impl From<HttpCacheError> for StreamingError {
    fn from(error: HttpCacheError) -> Self {
        match error {
            HttpCacheError::Streaming(streaming_err) => streaming_err,
            _ => Self::new(Box::new(error)),
        }
    }
}

/// Streaming error type specifically for client-specific streaming operations
///
/// This type provides a more granular error classification for streaming operations
/// while being compatible with the unified HttpCacheError system.
///
/// # Examples
///
/// ```rust
/// use http_cache::{ClientStreamingError, HttpCacheError};
///
/// // Create a streaming error with specific client context
/// let streaming_err = ClientStreamingError::client("reqwest", "Network timeout during streaming");
/// let cache_err: HttpCacheError = streaming_err.into();
/// ```
#[derive(Debug)]
pub enum ClientStreamingError {
    /// Client-specific streaming error with context
    Client {
        /// The name of the client library (e.g., "reqwest", "tower")
        client: String,
        /// The underlying client error
        error: BoxError,
    },
    /// HTTP cache streaming error (delegated to StreamingError)
    HttpCache(StreamingError),
    /// Other streaming error
    Other(BoxError),
}

impl ClientStreamingError {
    /// Create a client-specific streaming error
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_cache::ClientStreamingError;
    ///
    /// let err = ClientStreamingError::client("reqwest", "Connection timeout");
    /// ```
    pub fn client<C, E>(client: C, error: E) -> Self
    where
        C: Into<String>,
        E: Into<BoxError>,
    {
        Self::Client { client: client.into(), error: error.into() }
    }

    /// Create an HTTP cache streaming error
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_cache::{ClientStreamingError, StreamingError};
    ///
    /// let streaming_err = StreamingError::io("File read failed");
    /// let err = ClientStreamingError::http_cache(streaming_err);
    /// ```
    pub fn http_cache(error: StreamingError) -> Self {
        Self::HttpCache(error)
    }

    /// Create a generic streaming error
    ///
    /// # Examples
    ///
    /// ```rust
    /// use http_cache::ClientStreamingError;
    ///
    /// let err = ClientStreamingError::other("Unexpected streaming error");
    /// ```
    pub fn other<E: Into<BoxError>>(error: E) -> Self {
        Self::Other(error.into())
    }
}

impl fmt::Display for ClientStreamingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Client { client, error } => {
                write!(f, "{} streaming error: {}", client, error)
            }
            Self::HttpCache(e) => {
                write!(f, "HTTP cache streaming error: {}", e)
            }
            Self::Other(e) => write!(f, "Streaming error: {}", e),
        }
    }
}

impl std::error::Error for ClientStreamingError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Client { error, .. } => Some(error.as_ref()),
            Self::HttpCache(e) => Some(e),
            Self::Other(e) => Some(e.as_ref()),
        }
    }
}

impl From<StreamingError> for ClientStreamingError {
    fn from(error: StreamingError) -> Self {
        Self::HttpCache(error)
    }
}

impl From<BoxError> for ClientStreamingError {
    fn from(error: BoxError) -> Self {
        Self::Other(error)
    }
}

impl From<ClientStreamingError> for HttpCacheError {
    fn from(error: ClientStreamingError) -> Self {
        match error {
            ClientStreamingError::HttpCache(streaming_err) => {
                Self::Streaming(streaming_err)
            }
            ClientStreamingError::Client { error, .. } => Self::Client(error),
            ClientStreamingError::Other(error) => Self::Other(error),
        }
    }
}

impl From<ClientStreamingError> for StreamingError {
    fn from(error: ClientStreamingError) -> Self {
        match error {
            ClientStreamingError::HttpCache(streaming_err) => streaming_err,
            ClientStreamingError::Client { client, error } => {
                // Preserve client context by wrapping in a descriptive error
                let client_error =
                    format!("Client '{}' error: {}", client, error);
                Self::client(client_error)
            }
            ClientStreamingError::Other(error) => Self::new(error),
        }
    }
}