footing 0.1.1

Feature-gated foundations for Rust backend services
Documentation
//! HTTP policy and foundation construction errors.

use std::fmt;

use thiserror::Error;

use crate::{
    http::RateLimitError,
    result::{ClassifyError, ErrorKind},
};

#[derive(Error)]
#[non_exhaustive]
/// Errors that can occur while handling HTTP layer.
pub enum HttpLayerError {
    #[error("rate limiting is enabled but no rate-limit store was provided")]
    /// The enabled rate-limit policy has no runtime store.
    MissingRateLimitStore,
    #[error("idempotency is enabled but no idempotency store was provided")]
    /// The enabled idempotency policy has no runtime store.
    MissingIdempotencyStore,
    #[error("restricted CORS requires at least one allowed origin")]
    /// Indicates CORS allowed origins empty.
    CorsAllowedOriginsEmpty,
    #[error("CORS credentials cannot be enabled with wildcard origin")]
    /// Indicates CORS wildcard origin with credentials.
    CorsWildcardOriginWithCredentials,
    #[error("invalid HTTP header value in {field}")]
    /// The header value is invalid.
    InvalidHeaderValue {
        /// The configuration field name.
        field: &'static str,
        #[source]
        /// The source value.
        source: axum::http::header::InvalidHeaderValue,
    },
    #[error("invalid HTTP header name in {field}")]
    /// The header name is invalid.
    InvalidHeaderName {
        /// The configuration field name.
        field: &'static str,
        #[source]
        /// The source value.
        source: axum::http::header::InvalidHeaderName,
    },
    #[error("invalid HTTP method in {field}")]
    /// The method is invalid.
    InvalidMethod {
        #[doc = "The configuration field name."]
        field: &'static str,
        #[doc = "The offending value."]
        value: String,
    },
    #[error("invalid trusted proxy CIDR")]
    /// The trusted proxy CIDR is invalid.
    InvalidTrustedProxyCidr {
        #[doc = "The reason for this outcome."]
        reason: String,
    },
    #[error("invalid rate limit configuration")]
    /// The rate limit config is invalid.
    InvalidRateLimitConfig(#[from] RateLimitError),
    #[error("invalid rate limit key source in {field}")]
    /// The rate limit key source is invalid.
    InvalidRateLimitKeySource {
        #[doc = "The configuration field name."]
        field: &'static str,
    },
    #[error("invalid idempotency configuration")]
    /// The idempotency config is invalid.
    InvalidIdempotencyConfig,
    #[error("request timeout must be greater than zero")]
    /// The timeout is invalid.
    InvalidTimeout,
    #[error("request body limit must be greater than zero")]
    /// The request body limit is invalid.
    InvalidRequestBodyLimit,
    #[error("HTTP route matcher path must be an absolute path")]
    /// The route matcher is invalid.
    InvalidRouteMatcher,
}

impl fmt::Debug for HttpLayerError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("HttpLayerError")
            .field("code", &self.error_code())
            .finish_non_exhaustive()
    }
}

impl ClassifyError for HttpLayerError {
    fn error_kind(&self) -> ErrorKind {
        match self {
            Self::InvalidRateLimitConfig(source) => source.error_kind(),
            Self::MissingRateLimitStore
            | Self::MissingIdempotencyStore
            | Self::CorsAllowedOriginsEmpty
            | Self::CorsWildcardOriginWithCredentials
            | 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::CorsAllowedOriginsEmpty => "http_cors_origins_empty",
            Self::CorsWildcardOriginWithCredentials => "http_cors_wildcard_with_credentials",
            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",
        })
    }
}