httpd: Separate `Error` types of git route and `/raw`

Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
xphoniex 2023-03-13 12:22:48 +00:00 committed by Alexis Sellier
parent b80fc5437c
commit 3ee76c2891
No known key found for this signature in database
4 changed files with 39 additions and 23 deletions

View File

@ -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.

View File

@ -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);

View File

@ -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()

View File

@ -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;