Skip to content

Commit da22a27

Browse files
committed
include the rate limited action in the error message
1 parent 466afb6 commit da22a27

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/rate_limiter.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ impl LimitedAction {
2727
}
2828
}
2929

30+
pub fn error_message(&self) -> &'static str {
31+
match self {
32+
LimitedAction::PublishNew => {
33+
"You have published too many new crates in a short period of time."
34+
}
35+
}
36+
}
37+
3038
/// Key used to identify this action in environment variables. See `src/config.rs`.
3139
pub fn env_var_key(&self) -> &'static str {
3240
match self {
@@ -62,6 +70,7 @@ impl RateLimiter {
6270
Ok(())
6371
} else {
6472
Err(Box::new(TooManyRequests {
73+
action,
6574
retry_after: bucket.last_refill
6675
+ chrono::Duration::from_std(self.config[&action].rate).unwrap(),
6776
}))

src/util/errors/json.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt;
22

33
use super::{AppError, InternalAppErrorStatic};
4+
use crate::rate_limiter::LimitedAction;
45
use crate::util::{json_response, AppResponse};
56

67
use chrono::NaiveDateTime;
@@ -90,6 +91,7 @@ pub(super) struct BadRequest(pub(super) String);
9091
pub(super) struct ServerError(pub(super) String);
9192
#[derive(Debug)]
9293
pub(crate) struct TooManyRequests {
94+
pub action: LimitedAction,
9395
pub retry_after: NaiveDateTime,
9496
}
9597

@@ -137,9 +139,9 @@ impl AppError for TooManyRequests {
137139
let retry_after = self.retry_after.format(HTTP_DATE_FORMAT);
138140

139141
let detail = format!(
140-
"You have published too many crates in a \
141-
short period of time. Please try again after {} or email \
142+
"{} Please try again after {} or email \
142143
[email protected] to have your limit increased.",
144+
self.action.error_message(),
143145
retry_after
144146
);
145147
let mut response = json_error(&detail, StatusCode::TOO_MANY_REQUESTS);

0 commit comments

Comments
 (0)