Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
httpd: implement `IntoResponse` for api error variants
xphoniex committed 3 years ago
commit da4287258b132b17a476622ab5efbbdab0b6005d
parent 591b86aee5c1ab4c3d481f0d717ce389297bc1bd
1 file changed +25 -10
modified radicle-httpd/src/api/error.rs
@@ -1,5 +1,7 @@
-
use axum::http;
+
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
+
use axum::Json;
+
use serde_json::json;

/// Errors relating to the HTTP backend.
#[derive(Debug, thiserror::Error)]
@@ -73,16 +75,29 @@ pub enum Error {
    StorageRef(#[from] radicle::storage::refs::Error),
}

-
impl Error {
-
    pub fn status(&self) -> http::StatusCode {
-
        http::StatusCode::INTERNAL_SERVER_ERROR
-
    }
-
}
-

impl IntoResponse for Error {
    fn into_response(self) -> Response {
-
        tracing::error!("{}", self);
-

-
        self.status().into_response()
+
        let (status, msg) = match &self {
+
            Error::NotFound => (StatusCode::NOT_FOUND, None),
+
            Error::Auth(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())),
+
            Error::SiweParse(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())),
+
            Error::SiweVerification(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())),
+
            Error::Git2(e) => (
+
                StatusCode::INTERNAL_SERVER_ERROR,
+
                Some(e.message().to_owned()),
+
            ),
+
            _ => {
+
                tracing::error!("Error: {:?}", &self);
+

+
                (StatusCode::INTERNAL_SERVER_ERROR, None)
+
            }
+
        };
+

+
        let body = Json(json!({
+
            "error": msg.or_else(|| status.canonical_reason().map(|r| r.to_string())),
+
            "code": status.as_u16()
+
        }));
+

+
        (status, body).into_response()
    }
}