Skip to content

Commit 2d8cf4a

Browse files
committed
include the rate limited action in the error message
1 parent 8a21977 commit 2d8cf4a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/rate_limiter.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ impl LimitedAction {
3333
LimitedAction::PublishNew => "PUBLISH_NEW",
3434
}
3535
}
36+
37+
pub fn error_messagge(&self) -> &'static str {
38+
match self {
39+
LimitedAction::PublishNew => {
40+
"You have published too many new crates in a short period of time"
41+
}
42+
}
43+
}
3644
}
3745

3846
#[derive(Debug, Clone, Copy)]
@@ -62,6 +70,7 @@ impl RateLimiter {
6270
Ok(())
6371
} else {
6472
Err(Box::new(TooManyRequests {
73+
action: performed_action,
6574
retry_after: bucket.last_refill
6675
+ chrono::Duration::from_std(self.config_for_action(performed_action).rate)
6776
.unwrap(),

src/util/errors/json.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::fmt;
44

55
use super::{AppError, BoxedAppError, InternalAppErrorStatic};
66

7+
use crate::rate_limiter::LimitedAction;
78
use chrono::NaiveDateTime;
89
use http::{header, StatusCode};
910

@@ -74,6 +75,7 @@ pub(super) struct ServerError(pub(super) String);
7475
pub(crate) struct ServiceUnavailable(pub(super) String);
7576
#[derive(Debug)]
7677
pub(crate) struct TooManyRequests {
78+
pub action: LimitedAction,
7779
pub retry_after: NaiveDateTime,
7880
}
7981

@@ -131,9 +133,9 @@ impl AppError for TooManyRequests {
131133
let retry_after = self.retry_after.format(HTTP_DATE_FORMAT);
132134

133135
let detail = format!(
134-
"You have published too many crates in a \
135-
short period of time. Please try again after {retry_after} or email \
136-
[email protected] to have your limit increased."
136+
"{}. Please try again after {retry_after} or email \
137+
[email protected] to have your limit increased.",
138+
self.action.error_messagge()
137139
);
138140
let mut response = json_error(&detail, StatusCode::TOO_MANY_REQUESTS);
139141
response.headers_mut().insert(

0 commit comments

Comments
 (0)