use axum::body::Body;
use axum::http::header::CONTENT_TYPE;
use axum::http::HeaderValue;
use axum::response::{IntoResponse, Response};
pub use seiva_contract::{expose_error_details, set_expose_error_details, Problem};
pub const PROBLEM_JSON: &str = "application/problem+json";
#[derive(Debug, Clone)]
pub struct ProblemResponse(pub Problem);
impl From<Problem> for ProblemResponse {
fn from(problem: Problem) -> Self {
Self(problem)
}
}
impl IntoResponse for ProblemResponse {
fn into_response(self) -> Response {
let problem = self.0;
let body = serde_json::to_vec(&problem.to_json()).unwrap_or_else(|_| b"{}".to_vec());
let mut response = Response::new(Body::from(body));
*response.status_mut() = problem.status;
response
.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static(PROBLEM_JSON));
if let Some(request_id) = &problem.request_id {
if let Ok(value) = HeaderValue::from_str(request_id) {
response.headers_mut().insert("x-request-id", value);
}
}
response
}
}