athena_rs 3.23.0

Hyper performant polyglot Database driver
Documentation
//! `/debug/schema` service-layer error mapping helpers.
//!
//! This module converts lower-level client-resolution and snapshot-fetch
//! failures into the shared `LoggingSchemaDebugReportError` contract.

use actix_web::HttpResponse;

use super::debug_service_error::LoggingSchemaDebugReportError;
use super::debug_snapshot_error::LoggingSchemaSnapshotError;

/// Maps logging-client resolution failures into service-layer error contracts.
pub(super) fn map_logging_client_resolution_error(
    resp: HttpResponse,
) -> LoggingSchemaDebugReportError {
    LoggingSchemaDebugReportError::ResolveLoggingClient(resp)
}

/// Maps snapshot-fetch failures into service-layer error contracts.
pub(super) fn map_logging_snapshot_fetch_error(
    err: LoggingSchemaSnapshotError,
) -> LoggingSchemaDebugReportError {
    LoggingSchemaDebugReportError::FetchSnapshot(err)
}

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

    use super::*;
    use crate::api::response::service_unavailable;
    use crate::api::schema::debug_response_test_helpers::{
        assert_error_payload_for_tests, response_json_for_tests,
    };

    #[actix_web::test]
    /// Ensures client-resolution envelopes remain intact when wrapped as service errors.
    async fn mapper_wraps_client_resolution_failure_preserving_envelope_contract() {
        let err = map_logging_client_resolution_error(service_unavailable(
            "Logging store unavailable",
            "No athena logging client is configured.",
        ));

        match err {
            LoggingSchemaDebugReportError::ResolveLoggingClient(resp) => {
                assert_eq!(resp.status(), StatusCode::SERVICE_UNAVAILABLE);
                let body =
                    response_json_for_tests(resp, "service error mapper resolve client").await;

                assert_error_payload_for_tests(
                    &body,
                    "Logging store unavailable",
                    &["No athena logging client is configured."],
                    "service error mapper resolve client",
                );
            }
            LoggingSchemaDebugReportError::FetchSnapshot(_) => {
                panic!("expected ResolveLoggingClient variant")
            }
        }
    }

    #[test]
    /// Ensures snapshot-fetch failures preserve protocol detail text in service errors.
    fn mapper_wraps_snapshot_fetch_failure_preserving_protocol_message() {
        let err = map_logging_snapshot_fetch_error(LoggingSchemaSnapshotError::FetchColumns(
            sqlx::Error::Protocol("columns failed".to_string()),
        ));

        match err {
            LoggingSchemaDebugReportError::FetchSnapshot(
                LoggingSchemaSnapshotError::FetchColumns(source),
            ) => {
                assert!(
                    source.to_string().contains("columns failed"),
                    "expected protocol message in snapshot source: {source}"
                );
            }
            LoggingSchemaDebugReportError::FetchSnapshot(
                LoggingSchemaSnapshotError::FetchRelations(_),
            ) => {
                panic!("expected FetchColumns variant")
            }
            LoggingSchemaDebugReportError::ResolveLoggingClient(_) => {
                panic!("expected FetchSnapshot variant")
            }
        }
    }
}