Skip to main content

statespace_server/
error.rs

1//! Error types with HTTP status code mapping.
2
3use axum::http::StatusCode;
4use axum::response::{IntoResponse, Response};
5
6pub use statespace_tool_runtime::Error;
7pub type Result<T> = std::result::Result<T, Error>;
8
9pub trait ErrorExt {
10    fn status_code(&self) -> StatusCode;
11}
12
13impl ErrorExt for Error {
14    fn status_code(&self) -> StatusCode {
15        StatusCode::from_u16(self.http_status_code()).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
16    }
17}
18
19#[derive(Debug)]
20pub struct ServerError(pub Error);
21
22impl From<Error> for ServerError {
23    fn from(e: Error) -> Self {
24        Self(e)
25    }
26}
27
28impl IntoResponse for ServerError {
29    fn into_response(self) -> Response {
30        let status = self.0.status_code();
31        let body = self.0.user_message();
32        (status, body).into_response()
33    }
34}