Skip to main content

haste_fhir_operation_error/
axum.rs

1use crate::OperationOutcomeError;
2use axum::{
3    http::{HeaderValue, StatusCode, header},
4    response::{IntoResponse, Response},
5};
6use std::sync::Arc;
7
8impl OperationOutcomeError {
9    /// [`http_status`](Self::http_status) as a `StatusCode`.
10    #[must_use]
11    pub fn status(&self) -> StatusCode {
12        StatusCode::from_u16(self.http_status()).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
13    }
14}
15
16impl IntoResponse for OperationOutcomeError {
17    fn into_response(self) -> Response {
18        let status = self.status();
19        let body = serde_json::to_string(self.outcome()).expect("an OperationOutcome serializes");
20        let mut response = (
21            status,
22            [(
23                header::CONTENT_TYPE,
24                HeaderValue::from_static("application/fhir+json"),
25            )],
26            body,
27        )
28            .into_response();
29        // The logging and content negotiation middleware read the error back from here.
30        response.extensions_mut().insert(Arc::new(self));
31        response
32    }
33}