use actix_web::HttpResponse;
use actix_web::http::StatusCode;
use serde_json::Value;
use crate::api::gateway::contracts::GatewayOperationKind;
use athena_gateway::GatewayErrorEnvelope;
pub use athena_gateway::{
GATEWAY_ERROR_CODE_INTERNAL_ERROR, GATEWAY_ERROR_CODE_SERVICE_UNAVAILABLE,
GATEWAY_ERROR_CODE_VALIDATION_FAILED,
};
pub fn gateway_error_with_details(
status: StatusCode,
code: impl Into<String>,
operation: GatewayOperationKind,
message: impl Into<String>,
error: impl Into<String>,
details: Option<Value>,
) -> HttpResponse {
HttpResponse::build(status).json(GatewayErrorEnvelope::new(
code, operation, message, error, details,
))
}
pub fn gateway_bad_request_with_details(
operation: GatewayOperationKind,
message: impl Into<String>,
error: impl Into<String>,
details: Option<Value>,
) -> HttpResponse {
gateway_error_with_details(
StatusCode::BAD_REQUEST,
GATEWAY_ERROR_CODE_VALIDATION_FAILED,
operation,
message,
error,
details,
)
}
pub fn gateway_bad_request_with_code_and_details(
code: impl Into<String>,
operation: GatewayOperationKind,
message: impl Into<String>,
error: impl Into<String>,
details: Option<Value>,
) -> HttpResponse {
gateway_error_with_details(
StatusCode::BAD_REQUEST,
code,
operation,
message,
error,
details,
)
}
pub fn gateway_bad_request(
operation: GatewayOperationKind,
message: impl Into<String>,
error: impl Into<String>,
) -> HttpResponse {
gateway_bad_request_with_details(operation, message, error, None)
}
pub fn gateway_internal_error(
operation: GatewayOperationKind,
message: impl Into<String>,
error: impl Into<String>,
) -> HttpResponse {
gateway_error_with_details(
StatusCode::INTERNAL_SERVER_ERROR,
GATEWAY_ERROR_CODE_INTERNAL_ERROR,
operation,
message,
error,
None,
)
}
pub fn gateway_service_unavailable(
operation: GatewayOperationKind,
message: impl Into<String>,
error: impl Into<String>,
) -> HttpResponse {
gateway_error_with_details(
StatusCode::SERVICE_UNAVAILABLE,
GATEWAY_ERROR_CODE_SERVICE_UNAVAILABLE,
operation,
message,
error,
None,
)
}