use actix_web::HttpResponse;
use super::debug_service_error::LoggingSchemaDebugReportError;
use super::debug_snapshot_error::LoggingSchemaSnapshotError;
pub(super) fn map_logging_client_resolution_error(
resp: HttpResponse,
) -> LoggingSchemaDebugReportError {
LoggingSchemaDebugReportError::ResolveLoggingClient(resp)
}
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]
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]
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")
}
}
}
}