use std::fmt;
use thiserror::Error;
use crate::{
http::{EndpointIdempotency, EndpointMethod, IdempotencyRequestPolicy, RateLimitError},
result::{ClassifyError, ErrorKind},
};
#[derive(Error)]
#[non_exhaustive]
pub enum HttpMiddlewareError {
#[error("rate limiting is enabled but no rate-limit store was provided")]
MissingRateLimitStore,
#[error("idempotency is enabled but no idempotency store was provided")]
MissingIdempotencyStore,
#[error("principal rate limiting requires an HTTP authentication hook")]
MissingAuthenticationHook,
#[error(
"endpoint contract {method} {path} rate-limit policy differs: expected enabled={expected_enabled}, actual enabled={actual_enabled}"
)]
EndpointContractRateLimitMismatch {
method: EndpointMethod,
path: &'static str,
expected_enabled: bool,
actual_enabled: bool,
},
#[error(
"endpoint contract {method} {path} idempotency policy differs: expected {expected:?}, actual {actual:?}"
)]
EndpointContractIdempotencyMismatch {
method: EndpointMethod,
path: &'static str,
expected: EndpointIdempotency,
actual: IdempotencyRequestPolicy,
},
#[error("endpoint contract {method} {path} requires ordered authentication")]
EndpointContractAuthenticationRequired {
method: EndpointMethod,
path: &'static str,
},
#[error("public endpoint contract {method} {path} cannot use principal rate limiting")]
PublicEndpointPrincipalRateLimit {
method: EndpointMethod,
path: &'static str,
},
#[error("invalid HTTP header value in {field}")]
InvalidHeaderValue {
field: &'static str,
#[source]
source: axum::http::header::InvalidHeaderValue,
},
#[error("invalid HTTP header name in {field}")]
InvalidHeaderName {
field: &'static str,
#[source]
source: axum::http::header::InvalidHeaderName,
},
#[error("invalid HTTP method in {field}")]
InvalidMethod {
#[doc = "The configuration field name."]
field: &'static str,
#[doc = "The offending value."]
value: String,
},
#[error("invalid trusted proxy CIDR")]
InvalidTrustedProxyCidr {
#[doc = "The reason for this outcome."]
reason: String,
},
#[error("invalid rate limit configuration")]
InvalidRateLimitConfig(#[from] RateLimitError),
#[error("invalid rate limit key source in {field}")]
InvalidRateLimitKeySource {
#[doc = "The configuration field name."]
field: &'static str,
},
#[error("invalid idempotency configuration")]
InvalidIdempotencyConfig,
#[error("request timeout must be greater than zero")]
InvalidTimeout,
#[error("request body limit must be greater than zero")]
InvalidRequestBodyLimit,
#[error(
"HTTP route matcher must be an absolute ASCII path without whitespace, controls, query, fragment or backslash"
)]
InvalidRouteMatcher,
}
impl fmt::Debug for HttpMiddlewareError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("HttpMiddlewareError")
.field("code", &self.error_code())
.finish_non_exhaustive()
}
}
impl ClassifyError for HttpMiddlewareError {
fn error_kind(&self) -> ErrorKind {
match self {
Self::InvalidRateLimitConfig(source) => source.error_kind(),
Self::MissingRateLimitStore
| Self::MissingIdempotencyStore
| Self::MissingAuthenticationHook
| Self::EndpointContractRateLimitMismatch { .. }
| Self::EndpointContractIdempotencyMismatch { .. }
| Self::EndpointContractAuthenticationRequired { .. }
| Self::PublicEndpointPrincipalRateLimit { .. }
| Self::InvalidHeaderValue { .. }
| Self::InvalidHeaderName { .. }
| Self::InvalidMethod { .. }
| Self::InvalidTrustedProxyCidr { .. }
| Self::InvalidRateLimitKeySource { .. }
| Self::InvalidIdempotencyConfig
| Self::InvalidTimeout
| Self::InvalidRequestBodyLimit
| Self::InvalidRouteMatcher => ErrorKind::Validation,
}
}
fn error_code(&self) -> crate::result::ErrorCode {
crate::result::ErrorCode::internal(match self {
Self::MissingRateLimitStore => "http_rate_limit_store_required",
Self::MissingIdempotencyStore => "http_idempotency_store_required",
Self::MissingAuthenticationHook => "http_authentication_hook_required",
Self::EndpointContractRateLimitMismatch { .. } => {
"http_endpoint_contract_rate_limit_mismatch"
}
Self::EndpointContractIdempotencyMismatch { .. } => {
"http_endpoint_contract_idempotency_mismatch"
}
Self::EndpointContractAuthenticationRequired { .. } => {
"http_endpoint_contract_authentication_required"
}
Self::PublicEndpointPrincipalRateLimit { .. } => {
"http_public_endpoint_principal_rate_limit"
}
Self::InvalidHeaderValue { .. } => "http_invalid_header_value",
Self::InvalidHeaderName { .. } => "http_invalid_header_name",
Self::InvalidMethod { .. } => "http_invalid_method",
Self::InvalidTrustedProxyCidr { .. } => "http_invalid_trusted_proxy_cidr",
Self::InvalidRateLimitConfig(_) => "http_invalid_rate_limit",
Self::InvalidRateLimitKeySource { .. } => "http_invalid_rate_limit_key_source",
Self::InvalidIdempotencyConfig => "http_invalid_idempotency",
Self::InvalidTimeout => "http_invalid_timeout",
Self::InvalidRequestBodyLimit => "http_invalid_request_body_limit",
Self::InvalidRouteMatcher => "http_invalid_route_matcher",
})
}
}