Skip to main content

objectiveai_api/
error.rs

1//! Error response handling and conversion to Axum responses.
2
3/// Extension trait for converting [`objectiveai::error::ResponseError`] into Axum responses.
4#[cfg(not(target_arch = "wasm32"))]
5pub trait ResponseErrorExt {
6    /// Converts this error into an Axum HTTP response.
7    ///
8    /// The response status code is derived from the error's `code` field,
9    /// falling back to 500 Internal Server Error if the code is invalid.
10    /// The response body is the JSON-serialized error.
11    fn into_response(self) -> axum::response::Response;
12}
13
14#[cfg(not(target_arch = "wasm32"))]
15impl ResponseErrorExt for objectiveai::error::ResponseError {
16    fn into_response(self) -> axum::response::Response {
17        use axum::response::IntoResponse;
18        let status = axum::http::StatusCode::from_u16(self.code)
19            .unwrap_or(axum::http::StatusCode::INTERNAL_SERVER_ERROR);
20        let body = serde_json::to_string(&self).unwrap_or_default();
21        println!("ResponseError: {}", body);
22        (status, body).into_response()
23    }
24}