Skip to main content

tower_rate_limiter/limiter/
error.rs

1//! Error types for the rate limiter.
2
3use std::time::Duration;
4
5/// A failure produced while resolving or charging a rate-limit policy.
6#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
7pub enum RateLimitError {
8    /// The client key could not be resolved.
9    #[error("client key unavailable ({0}): {1}")]
10    Key(String, String),
11    /// The request's quota limit could not be resolved.
12    #[error("rate-limit quota unavailable ({0}): {1}")]
13    Quota(String, String),
14    /// The rate-limit store could not charge the request reliably.
15    #[error("rate-limit store unavailable ({0}): {1}")]
16    Store(String, String),
17}
18
19impl RateLimitError {
20    /// Return the stable machine-readable error code.
21    pub fn code(&self) -> &str {
22        match self {
23            Self::Key(code, _) | Self::Quota(code, _) | Self::Store(code, _) => code,
24        }
25    }
26}
27
28/// Errors produced while validating a rate-limit builder.
29#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
30pub enum ConfigError {
31    /// The configured window is shorter than the one-millisecond minimum.
32    #[error("rate-limit window {0:?} is shorter than the minimum {1:?}")]
33    WindowTooShort(Duration, Duration),
34    /// The policy identifier is invalid.
35    #[error("empty policy name")]
36    EmptyPolicyName,
37}