athena_rs 3.26.1

Hyper performant polyglot Database driver
Documentation
//! Report-result response assembly for `/debug/schema`.
//!
//! This module maps a service-layer report result into a route-ready HTTP
//! response while delegating success and error payload shaping to focused
//! handoff helpers.

use actix_web::HttpResponse;

use super::debug_contracts::LoggingSchemaDebugReport;
use super::debug_response_output_assembly::debug_schema_success_response;
use super::debug_response_result_error_handoff_assembly::build_debug_response_error_handoff_from_report_result;
use super::debug_service_error::LoggingSchemaDebugReportError;

/// Builds a route-ready response from one debug-report loading result.
pub(super) fn build_debug_schema_response_from_report_result(
    report_result: Result<LoggingSchemaDebugReport, LoggingSchemaDebugReportError>,
) -> HttpResponse {
    match report_result {
        Ok(report) => debug_schema_success_response(report),
        Err(err) => build_debug_response_error_handoff_from_report_result(err),
    }
}

#[cfg(test)]
mod tests {
    use actix_web::http::StatusCode;

    use super::*;
    use crate::api::schema::debug_report_test_fixtures::build_empty_logging_schema_debug_report_for_tests;
    use crate::api::schema::debug_response_stage_test_helpers::{
        build_fetch_columns_service_error_for_tests,
        build_resolve_logging_client_service_error_for_tests,
    };
    use crate::api::schema::debug_response_test_helpers::{
        assert_error_payload_for_tests, assert_success_payload_field_for_tests,
        response_json_for_tests,
    };

    #[actix_web::test]
    /// Maps successful report results to HTTP 200 responses with report payloads.
    async fn result_assembly_maps_success_to_ok_response() {
        let report = build_empty_logging_schema_debug_report_for_tests();
        let response = build_debug_schema_response_from_report_result(Ok(report));
        assert_eq!(response.status(), StatusCode::OK);
        let body = response_json_for_tests(response, "result-assembly").await;

        assert_success_payload_field_for_tests(&body, "summary", "result-assembly");
    }

    #[actix_web::test]
    /// Maps snapshot-fetch report failures to HTTP 500 responses with error payloads.
    async fn result_assembly_maps_snapshot_failure_to_internal_error_response() {
        let response = build_debug_schema_response_from_report_result(Err(
            build_fetch_columns_service_error_for_tests("fetch failure"),
        ));
        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
        let body = response_json_for_tests(response, "result-assembly").await;

        assert_error_payload_for_tests(
            &body,
            "Failed to fetch logging schema columns",
            &["Failed to fetch logging schema columns:", "fetch failure"],
            "result-assembly",
        );
    }

    #[actix_web::test]
    /// Preserves logging-client resolution failure envelopes through result-assembly handoff.
    async fn result_assembly_preserves_resolve_logging_client_failure_envelope() {
        let response = build_debug_schema_response_from_report_result(Err(
            build_resolve_logging_client_service_error_for_tests(),
        ));
        assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
        let body = response_json_for_tests(response, "result-assembly").await;

        assert_error_payload_for_tests(
            &body,
            "Logging store unavailable",
            &["No athena logging client is configured."],
            "result-assembly",
        );
    }
}