From 3ee76c289177c0ab91d8367805fe31165b0589ee Mon Sep 17 00:00:00 2001 From: xphoniex Date: Mon, 13 Mar 2023 12:22:48 +0000 Subject: [PATCH] httpd: Separate `Error` types of git route and `/raw` Signed-off-by: xphoniex --- radicle-httpd/src/api/error.rs | 2 +- radicle-httpd/src/error.rs | 56 ++++++++++++++++++++++------------ radicle-httpd/src/git.rs | 2 +- radicle-httpd/src/raw.rs | 2 +- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/radicle-httpd/src/api/error.rs b/radicle-httpd/src/api/error.rs index 91d593a1..1edb48ff 100644 --- a/radicle-httpd/src/api/error.rs +++ b/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. diff --git a/radicle-httpd/src/error.rs b/radicle-httpd/src/error.rs index 29ada51d..8bd18ce0 100644 --- a/radicle-httpd/src/error.rs +++ b/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), - - /// Surf error. - #[error(transparent)] - Surf(#[from] radicle_surf::Error), - - // Surf file error. - #[error(transparent)] - SurfFile(#[from] radicle_surf::fs::error::File), } -impl Error { +impl GitError { 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, + 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 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. + #[error(transparent)] + SurfFile(#[from] radicle_surf::fs::error::File), +} + +impl RawError { + pub fn status(&self) -> http::StatusCode { + match self { + RawError::SurfFile(_) => http::StatusCode::NOT_FOUND, + _ => http::StatusCode::INTERNAL_SERVER_ERROR, + } + } +} + +impl IntoResponse for RawError { fn into_response(self) -> Response { tracing::error!("{}", self); diff --git a/radicle-httpd/src/git.rs b/radicle-httpd/src/git.rs index 4be5946a..6b9a521d 100644 --- a/radicle-httpd/src/git.rs +++ b/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, aliases: HashMap) -> Router { Router::new() diff --git a/radicle-httpd/src/raw.rs b/radicle-httpd/src/raw.rs index 9bc87a37..cde23ede 100644 --- a/radicle-httpd/src/raw.rs +++ b/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;