Skip to content

Commit bdd4402

Browse files
committed
switch to a json error message
1 parent 5214640 commit bdd4402

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

src/router.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::sync::Arc;
22

3-
use conduit::{box_error, header, Body, Handler, HandlerResult, RequestExt, Response, StatusCode};
3+
use conduit::{Handler, HandlerResult, RequestExt};
44
use conduit_router::{RequestParams, RouteBuilder, RoutePattern};
55

66
use crate::controllers::*;
77
use crate::middleware::app::RequestApp;
8-
use crate::util::errors::{std_error, AppError};
8+
use crate::util::errors::{std_error, AppError, RouteBlocked};
99
use crate::util::EndpointResult;
1010
use crate::{App, Env};
1111

@@ -156,13 +156,7 @@ impl Handler for C {
156156
// `RoutePattern` before executing the response handler.
157157
if let Some(pattern) = req.extensions().find::<RoutePattern>() {
158158
if req.app().config.blocked_routes.contains(pattern.pattern()) {
159-
let body = "This route is temporarily blocked. See https://status.crates.io";
160-
161-
return Response::builder()
162-
.status(StatusCode::SERVICE_UNAVAILABLE)
163-
.header(header::CONTENT_LENGTH, body.len())
164-
.body(Body::from_vec(body.as_bytes().to_vec()))
165-
.map_err(box_error);
159+
return Ok(RouteBlocked.response().unwrap());
166160
}
167161
}
168162

src/util/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mod json;
2727
pub use json::TOKEN_FORMAT_ERROR;
2828
pub(crate) use json::{
2929
InsecurelyGeneratedTokenRevoked, MetricsDisabled, NotFound, OwnershipInvitationExpired,
30-
ReadOnlyMode, TooManyRequests,
30+
ReadOnlyMode, RouteBlocked, TooManyRequests,
3131
};
3232

3333
/// Returns an error with status 200 and the provided description as JSON

src/util/errors/json.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,21 @@ impl fmt::Display for MetricsDisabled {
257257
f.write_str("Metrics are disabled on this crates.io instance")
258258
}
259259
}
260+
261+
#[derive(Debug)]
262+
pub(crate) struct RouteBlocked;
263+
264+
impl AppError for RouteBlocked {
265+
fn response(&self) -> Option<AppResponse> {
266+
Some(json_error(
267+
&self.to_string(),
268+
StatusCode::SERVICE_UNAVAILABLE,
269+
))
270+
}
271+
}
272+
273+
impl fmt::Display for RouteBlocked {
274+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
275+
f.write_str("This route is temporarily blocked. See https://status.crates.io.")
276+
}
277+
}

0 commit comments

Comments
 (0)