File tree Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,14 @@ impl LimitedAction {
33
33
LimitedAction :: PublishNew => "PUBLISH_NEW" ,
34
34
}
35
35
}
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
+ }
36
44
}
37
45
38
46
#[ derive( Debug , Clone , Copy ) ]
@@ -62,6 +70,7 @@ impl RateLimiter {
62
70
Ok ( ( ) )
63
71
} else {
64
72
Err ( Box :: new ( TooManyRequests {
73
+ action : performed_action,
65
74
retry_after : bucket. last_refill
66
75
+ chrono:: Duration :: from_std ( self . config_for_action ( performed_action) . rate )
67
76
. unwrap ( ) ,
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ use std::fmt;
4
4
5
5
use super :: { AppError , BoxedAppError , InternalAppErrorStatic } ;
6
6
7
+ use crate :: rate_limiter:: LimitedAction ;
7
8
use chrono:: NaiveDateTime ;
8
9
use http:: { header, StatusCode } ;
9
10
@@ -74,6 +75,7 @@ pub(super) struct ServerError(pub(super) String);
74
75
pub ( crate ) struct ServiceUnavailable ( pub ( super ) String ) ;
75
76
#[ derive( Debug ) ]
76
77
pub ( crate ) struct TooManyRequests {
78
+ pub action : LimitedAction ,
77
79
pub retry_after : NaiveDateTime ,
78
80
}
79
81
@@ -131,9 +133,9 @@ impl AppError for TooManyRequests {
131
133
let retry_after = self . retry_after . format ( HTTP_DATE_FORMAT ) ;
132
134
133
135
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 ( )
137
139
) ;
138
140
let mut response = json_error ( & detail, StatusCode :: TOO_MANY_REQUESTS ) ;
139
141
response. headers_mut ( ) . insert (
You can’t perform that action at this time.
0 commit comments