athena_rs 3.26.2

Hyper performant polyglot Database driver
Documentation
//! `/debug/schema` error-to-response mapping helpers.
//!
//! This module converts report-loading service failures into stable
//! route-ready HTTP responses while delegating variant-specific mapping to
//! `debug_response_mapper_service_error_variants`.

use actix_web::HttpResponse;

use super::debug_response_mapper_service_error_variants::{
    map_fetch_snapshot_service_error_response, map_resolve_logging_client_service_error_response,
};
use super::debug_service_error::LoggingSchemaDebugReportError;

/// Maps report-loading failures into route-ready HTTP responses.
pub(super) fn debug_report_error_response(err: LoggingSchemaDebugReportError) -> HttpResponse {
    match err {
        LoggingSchemaDebugReportError::ResolveLoggingClient(resp) => {
            map_resolve_logging_client_service_error_response(resp)
        }
        LoggingSchemaDebugReportError::FetchSnapshot(source) => {
            map_fetch_snapshot_service_error_response(source)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::super::debug_snapshot_error::LoggingSchemaSnapshotError;
    use super::*;
    use crate::api::schema::debug_response_test_helpers::{
        assert_error_payload_for_tests, response_json_for_tests,
    };

    /// Ensures prebuilt client-resolution failures preserve their HTTP status.
    #[test]
    fn debug_report_error_response_passes_through_client_resolution_status() {
        let response =
            debug_report_error_response(LoggingSchemaDebugReportError::ResolveLoggingClient(
                HttpResponse::ServiceUnavailable().finish(),
            ));
        assert_eq!(
            response.status(),
            actix_web::http::StatusCode::SERVICE_UNAVAILABLE
        );
    }

    /// Ensures snapshot-fetch failures preserve the internal-error payload contract.
    #[actix_web::test]
    async fn debug_report_error_response_maps_snapshot_failure_to_internal_error() {
        let response = debug_report_error_response(LoggingSchemaDebugReportError::FetchSnapshot(
            LoggingSchemaSnapshotError::FetchRelations(sqlx::Error::Protocol(
                "snapshot failure".to_string(),
            )),
        ));
        assert_eq!(
            response.status(),
            actix_web::http::StatusCode::INTERNAL_SERVER_ERROR
        );
        let body = response_json_for_tests(response, "debug-response mapper").await;

        assert_error_payload_for_tests(
            &body,
            "Failed to fetch logging schema tables",
            &["Failed to fetch logging schema tables:", "snapshot failure"],
            "debug-response mapper",
        );
    }
}