use std::fmt;
use thiserror::Error;
use crate::{
http::{EndpointMethod, IdempotencyRequestPolicy},
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("identity-based 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: String,
expected_enabled: bool,
actual_enabled: bool,
},
#[error(
"endpoint contract {method} {path} idempotency policy differs: expected {expected:?}, actual {actual:?}"
)]
EndpointContractIdempotencyMismatch {
method: EndpointMethod,
path: String,
expected: IdempotencyRequestPolicy,
actual: IdempotencyRequestPolicy,
},
#[error("endpoint contract {method} {path} requires ordered authentication")]
EndpointContractAuthenticationRequired {
method: EndpointMethod,
path: String,
},
#[error("endpoint contract {method} {path} requires authorization before idempotency replay")]
EndpointContractAuthorizationRequired {
method: EndpointMethod,
path: String,
},
#[error("public endpoint contract {method} {path} cannot use identity-based rate limiting")]
PublicEndpointIdentityRateLimit {
method: EndpointMethod,
path: String,
},
#[error("invalid HTTP header name in {field}")]
InvalidHeaderName {
field: &'static str,
#[source]
source: axum::http::header::InvalidHeaderName,
},
#[error("invalid rate limit key source in {field}")]
InvalidRateLimitKeySource {
#[doc = "The configuration field name."]
field: &'static str,
},
#[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 {
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::EndpointContractAuthorizationRequired { .. } => {
"http_endpoint_contract_authorization_required"
}
Self::PublicEndpointIdentityRateLimit { .. } => {
"http_public_endpoint_identity_rate_limit"
}
Self::InvalidHeaderName { .. } => "http_invalid_header_name",
Self::InvalidRateLimitKeySource { .. } => "http_invalid_rate_limit_key_source",
Self::InvalidRequestBodyLimit => "http_invalid_request_body_limit",
Self::InvalidRouteMatcher => "http_invalid_route_matcher",
})
}
}