Skip to content

Commit e297454

Browse files
committed
middleware/block_traffic: Fix "large Err variant" warnings
1 parent b036c8e commit e297454

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/middleware/block_traffic.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::app::AppState;
22
use crate::middleware::log_request::RequestLogExt;
33
use crate::middleware::real_ip::RealIp;
4-
use crate::util::errors::custom;
4+
use crate::util::errors::{BoxedAppError, custom};
55
use axum::extract::{Extension, MatchedPath, Request};
66
use axum::middleware::Next;
77
use axum::response::{IntoResponse, Response};
@@ -14,9 +14,9 @@ pub async fn middleware(
1414
req: Request,
1515
next: Next,
1616
) -> Result<impl IntoResponse, Response> {
17-
block_by_ip(&real_ip, &state, req.headers())?;
18-
block_by_header(&state, &req)?;
19-
block_routes(matched_path.as_ref(), &state)?;
17+
block_by_ip(&real_ip, &state, req.headers()).map_err(IntoResponse::into_response)?;
18+
block_by_header(&state, &req).map_err(IntoResponse::into_response)?;
19+
block_routes(matched_path.as_ref(), &state).map_err(IntoResponse::into_response)?;
2020

2121
Ok(next.run(req).await)
2222
}
@@ -29,7 +29,7 @@ pub async fn middleware(
2929
/// to `User-Agent=BLOCKED_UAS` and `BLOCKED_UAS` to `curl/7.54.0,cargo 1.36.0 (c4fcfb725 2019-05-15)`
3030
/// to block requests from the versions of curl or Cargo specified (values are nonsensical examples).
3131
/// Values of the headers must match exactly.
32-
pub fn block_by_header(state: &AppState, req: &Request) -> Result<(), Response> {
32+
pub fn block_by_header(state: &AppState, req: &Request) -> Result<(), impl IntoResponse> {
3333
let blocked_traffic = &state.config.blocked_traffic;
3434

3535
for (header_name, blocked_values) in blocked_traffic {
@@ -53,15 +53,15 @@ pub fn block_by_ip(
5353
real_ip: &RealIp,
5454
state: &AppState,
5555
headers: &HeaderMap,
56-
) -> Result<(), Response> {
56+
) -> Result<(), impl IntoResponse> {
5757
if state.config.blocked_ips.contains(real_ip) {
5858
return Err(rejection_response_from(state, headers));
5959
}
6060

6161
Ok(())
6262
}
6363

64-
fn rejection_response_from(state: &AppState, headers: &HeaderMap) -> Response {
64+
fn rejection_response_from(state: &AppState, headers: &HeaderMap) -> impl IntoResponse {
6565
let domain_name = &state.config.domain_name;
6666

6767
// Heroku should always set this header
@@ -77,17 +77,19 @@ fn rejection_response_from(state: &AppState, headers: &HeaderMap) -> Response {
7777
Please email help@crates.io and provide the request id {request_id}"
7878
);
7979

80-
(StatusCode::FORBIDDEN, body).into_response()
80+
(StatusCode::FORBIDDEN, body)
8181
}
8282

8383
/// Allow blocking individual routes by their pattern through the `BLOCKED_ROUTES`
8484
/// environment variable.
85-
pub fn block_routes(matched_path: Option<&MatchedPath>, state: &AppState) -> Result<(), Response> {
85+
pub fn block_routes(
86+
matched_path: Option<&MatchedPath>,
87+
state: &AppState,
88+
) -> Result<(), BoxedAppError> {
8689
if let Some(matched_path) = matched_path {
8790
if state.config.blocked_routes.contains(matched_path.as_str()) {
8891
let body = "This route is temporarily blocked. See https://status.crates.io.";
89-
let error = custom(StatusCode::SERVICE_UNAVAILABLE, body);
90-
return Err(error.into_response());
92+
return Err(custom(StatusCode::SERVICE_UNAVAILABLE, body));
9193
}
9294
}
9395

0 commit comments

Comments
 (0)