use axum::{
Json,
http::{StatusCode, header},
response::{IntoResponse, Response},
};
use serde::Serialize;
#[derive(Clone, Debug)]
pub struct RequestId(pub String);
#[derive(Clone, Debug, Serialize)]
pub struct Problem {
#[serde(rename = "type")]
pub kind: &'static str,
pub title: &'static str,
pub status: u16,
pub code: &'static str,
pub request_id: String,
}
impl Problem {
pub(super) fn response(status: StatusCode, code: &'static str, id: &RequestId) -> Response {
let problem = Self {
kind: "about:blank",
title: status.canonical_reason().unwrap_or("Request failed"),
status: status.as_u16(),
code,
request_id: id.0.clone(),
};
let mut response = (status, Json(problem)).into_response();
response.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/problem+json"),
);
response.headers_mut().insert(
header::CONNECTION,
header::HeaderValue::from_static("close"),
);
response
}
}