footing 0.1.18

Feature-gated foundations for Rust backend services
Documentation
use std::{error::Error as StdError, fmt};

#[derive(Debug)]
#[non_exhaustive]
/// Errors that can occur while handling testkit.
pub enum TestkitError {
    /// Indicates request build.
    RequestBuild(axum::http::Error),
    /// Indicates response body.
    ResponseBody(axum::Error),
    /// Indicates response UTF-8.
    ResponseUtf8(std::string::FromUtf8Error),
    /// Indicates response JSON.
    ResponseJson(serde_json::Error),
    /// The problem body status differs from the HTTP response status.
    ResponseStatusMismatch {
        /// Status of the HTTP response.
        http_status: u16,
        /// Status declared by the problem body.
        problem_status: u16,
    },
}

impl fmt::Display for TestkitError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::RequestBuild(source) => write!(formatter, "test request build failed: {source}"),
            Self::ResponseBody(source) => write!(formatter, "response body read failed: {source}"),
            Self::ResponseUtf8(source) => write!(formatter, "response body is not UTF-8: {source}"),
            Self::ResponseJson(source) => write!(formatter, "response body is not JSON: {source}"),
            Self::ResponseStatusMismatch {
                http_status,
                problem_status,
            } => write!(
                formatter,
                "problem status {problem_status} does not match HTTP status {http_status}"
            ),
        }
    }
}

impl StdError for TestkitError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Self::RequestBuild(source) => Some(source),
            Self::ResponseBody(source) => Some(source),
            Self::ResponseUtf8(source) => Some(source),
            Self::ResponseJson(source) => Some(source),
            Self::ResponseStatusMismatch { .. } => None,
        }
    }
}