objectstore_server/
error.rs

1//!
2//! This is mostly adapted from <https://github.com/tokio-rs/axum/blob/main/examples/anyhow-error-response/src/main.rs>
3
4use axum::http::StatusCode;
5use axum::response::{IntoResponse, Response};
6
7pub enum AnyhowResponse {
8    Error(anyhow::Error),
9    Response(Response),
10}
11
12pub type ApiResult<T> = std::result::Result<T, AnyhowResponse>;
13
14impl IntoResponse for AnyhowResponse {
15    fn into_response(self) -> Response {
16        match self {
17            AnyhowResponse::Error(error) => {
18                tracing::error!(
19                    error = error.as_ref() as &dyn std::error::Error,
20                    "error handling request"
21                );
22
23                // TODO: Support more nuanced return codes for validation errors etc. See
24                // Relay's ApiErrorResponse and BadStoreRequest as examples.
25                StatusCode::INTERNAL_SERVER_ERROR.into_response()
26            }
27            AnyhowResponse::Response(response) => response,
28        }
29    }
30}
31
32impl From<Response> for AnyhowResponse {
33    fn from(response: Response) -> Self {
34        Self::Response(response)
35    }
36}
37
38impl From<anyhow::Error> for AnyhowResponse {
39    fn from(err: anyhow::Error) -> Self {
40        Self::Error(err)
41    }
42}