Skip to content

Commit 1f749d9

Browse files
committed
util/errors: Replace MetricsDisabled with CustomApiError
1 parent 60d4631 commit 1f749d9

File tree

3 files changed

+4
-20
lines changed

3 files changed

+4
-20
lines changed

src/controllers/metrics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::controllers::frontend_prelude::*;
2-
use crate::util::errors::{forbidden, not_found, MetricsDisabled};
2+
use crate::util::errors::{custom, forbidden, not_found};
33
use prometheus::TextEncoder;
44

55
/// Handles the `GET /api/private/metrics/:kind` endpoint.
@@ -17,7 +17,8 @@ pub async fn prometheus(app: AppState, Path(kind): Path<String>, req: Parts) ->
1717
} else {
1818
// To avoid accidentally leaking metrics if the environment variable is not set, prevent
1919
// access to any metrics endpoint if the authorization token is not configured.
20-
return Err(Box::new(MetricsDisabled));
20+
let detail = "Metrics are disabled on this crates.io instance";
21+
return Err(custom(StatusCode::NOT_FOUND, detail));
2122
}
2223

2324
let metrics = spawn_blocking(move || match kind.as_str() {

src/util/errors.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ mod json;
3232
use crate::email::EmailError;
3333
use crates_io_github::GitHubError;
3434
pub use json::TOKEN_FORMAT_ERROR;
35-
pub(crate) use json::{
36-
custom, InsecurelyGeneratedTokenRevoked, MetricsDisabled, ReadOnlyMode, TooManyRequests,
37-
};
35+
pub(crate) use json::{custom, InsecurelyGeneratedTokenRevoked, ReadOnlyMode, TooManyRequests};
3836

3937
pub type BoxedAppError = Box<dyn AppError>;
4038

src/util/errors/json.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,3 @@ impl fmt::Display for InsecurelyGeneratedTokenRevoked {
133133
Result::Ok(())
134134
}
135135
}
136-
137-
#[derive(Debug)]
138-
pub(crate) struct MetricsDisabled;
139-
140-
impl AppError for MetricsDisabled {
141-
fn response(&self) -> Response {
142-
json_error(&self.to_string(), StatusCode::NOT_FOUND)
143-
}
144-
}
145-
146-
impl fmt::Display for MetricsDisabled {
147-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148-
f.write_str("Metrics are disabled on this crates.io instance")
149-
}
150-
}

0 commit comments

Comments
 (0)