Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
httpd: Separate `Error` types of git route and `/raw`
xphoniex committed 3 years ago
commit 6ac94ca1d4c685fff4b1f1c390d0c9a7cdebb189
parent f68f0af76fb526fbe9fd4a72700cc1977191b7b6
4 files changed +32 -16
modified radicle-httpd/src/api/error.rs
@@ -3,7 +3,7 @@ use axum::response::{IntoResponse, Response};
use axum::Json;
use serde_json::json;

-
/// Errors relating to the HTTP backend.
+
/// Errors relating to the API backend.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// The entity was not found.
modified radicle-httpd/src/error.rs
@@ -1,9 +1,9 @@
use axum::http;
use axum::response::{IntoResponse, Response};

-
/// Errors relating to the HTTP backend.
+
/// Errors relating to the Git backend.
#[derive(Debug, thiserror::Error)]
-
pub enum Error {
+
pub enum GitError {
    /// The entity was not found.
    #[error("not found")]
    NotFound,
@@ -24,10 +24,6 @@ pub enum Error {
    #[error("backend error")]
    Backend,

-
    /// Id is not valid.
-
    #[error("id is not valid")]
-
    InvalidId,
-

    /// HeaderName error.
    #[error(transparent)]
    InvalidHeaderName(#[from] axum::http::header::InvalidHeaderName),
@@ -35,29 +31,49 @@ pub enum Error {
    /// HeaderValue error.
    #[error(transparent)]
    InvalidHeaderValue(#[from] axum::http::header::InvalidHeaderValue),
+
}
+

+
impl GitError {
+
    pub fn status(&self) -> http::StatusCode {
+
        match self {
+
            GitError::ServiceUnavailable(_) => http::StatusCode::SERVICE_UNAVAILABLE,
+
            GitError::Id(_) => http::StatusCode::NOT_FOUND,
+
            GitError::NotFound => http::StatusCode::NOT_FOUND,
+
            _ => http::StatusCode::INTERNAL_SERVER_ERROR,
+
        }
+
    }
+
}

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

+
        self.status().into_response()
+
    }
+
}
+

+
/// Errors relating to the `/raw` route.
+
#[derive(Debug, thiserror::Error)]
+
pub enum RawError {
    /// Surf error.
    #[error(transparent)]
    Surf(#[from] radicle_surf::Error),

-
    // Surf file error.
+
    /// Surf file error.
    #[error(transparent)]
    SurfFile(#[from] radicle_surf::fs::error::File),
}

-
impl Error {
+
impl RawError {
    pub fn status(&self) -> http::StatusCode {
        match self {
-
            Error::ServiceUnavailable(_) => http::StatusCode::SERVICE_UNAVAILABLE,
-
            Error::InvalidId => http::StatusCode::NOT_FOUND,
-
            Error::Id(_) => http::StatusCode::NOT_FOUND,
-
            Error::NotFound => http::StatusCode::NOT_FOUND,
+
            RawError::SurfFile(_) => http::StatusCode::NOT_FOUND,
            _ => http::StatusCode::INTERNAL_SERVER_ERROR,
        }
    }
}

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

modified radicle-httpd/src/git.rs
@@ -19,7 +19,7 @@ use hyper::body::Buf as _;
use radicle::identity::Id;
use radicle::profile::Profile;

-
use crate::error::Error;
+
use crate::error::GitError as Error;

pub fn router(profile: Arc<Profile>, aliases: HashMap<String, Id>) -> Router {
    Router::new()
modified radicle-httpd/src/raw.rs
@@ -13,7 +13,7 @@ use radicle::storage::git::paths;
use radicle_surf::{Oid, Repository};

use crate::axum_extra::Path;
-
use crate::error::Error;
+
use crate::error::RawError as Error;

const MAX_BLOB_SIZE: usize = 4_194_304;