uv/crates/uv-client/src/middleware.rs
Charlie Marsh c996e8e3f3
Enable workspace lint configuration in remaining crates (#4329)
## Summary

We didn't have Clippy enabled (to match our workspace settings) in a few
crates.
2024-06-18 03:02:28 +00:00

47 lines
1.2 KiB
Rust

use http::Extensions;
use std::fmt::Debug;
use reqwest::{Request, Response};
use reqwest_middleware::{Middleware, Next};
use url::Url;
/// A custom error type for the offline middleware.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct OfflineError {
url: Url,
}
impl OfflineError {
/// Returns the URL that caused the error.
pub(crate) fn url(&self) -> &Url {
&self.url
}
}
impl std::fmt::Display for OfflineError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Network connectivity is disabled, but the requested data wasn't found in the cache for: `{}`", self.url)
}
}
impl std::error::Error for OfflineError {}
/// A middleware that always returns an error indicating that the client is offline.
pub(crate) struct OfflineMiddleware;
#[async_trait::async_trait]
impl Middleware for OfflineMiddleware {
async fn handle(
&self,
req: Request,
_extensions: &mut Extensions,
_next: Next<'_>,
) -> reqwest_middleware::Result<Response> {
Err(reqwest_middleware::Error::Middleware(
OfflineError {
url: req.url().clone(),
}
.into(),
))
}
}