From 5a5ab7ad52fc471055264c6400462f37c2de2d65 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 14 Jul 2023 13:46:53 +0200 Subject: [PATCH] feat(server): start proper error handling --- server/src/error.rs | 45 ++++++++++++++++++++++++++++++++++++++++++ server/src/main.rs | 3 +++ server/src/repo/mod.rs | 26 ++++++++---------------- 3 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 server/src/error.rs diff --git a/server/src/error.rs b/server/src/error.rs new file mode 100644 index 0000000..8993dec --- /dev/null +++ b/server/src/error.rs @@ -0,0 +1,45 @@ +use axum::http::StatusCode; +use axum::response::{IntoResponse, Response}; +use std::error::Error; +use std::fmt; +use std::io; + +pub type Result = std::result::Result; + +#[derive(Debug)] +pub enum ServerError { + IO(io::Error), + Axum(axum::Error), +} + +impl fmt::Display for ServerError { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ServerError::IO(err) => write!(fmt, "{}", err), + ServerError::Axum(err) => write!(fmt, "{}", err), + } + } +} + +impl Error for ServerError {} + +impl IntoResponse for ServerError { + fn into_response(self) -> Response { + match self { + ServerError::IO(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(), + ServerError::Axum(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(), + } + } +} + +impl From for ServerError { + fn from(err: io::Error) -> Self { + ServerError::IO(err) + } +} + +impl From for ServerError { + fn from(err: axum::Error) -> Self { + ServerError::Axum(err) + } +} diff --git a/server/src/main.rs b/server/src/main.rs index f8a959d..de80e2f 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,5 +1,8 @@ +mod error; mod repo; +pub use error::{Result, ServerError}; + use axum::extract::FromRef; use axum::Router; use repo::RepoGroupManager; diff --git a/server/src/repo/mod.rs b/server/src/repo/mod.rs index 3e221a2..265f677 100644 --- a/server/src/repo/mod.rs +++ b/server/src/repo/mod.rs @@ -34,35 +34,25 @@ pub fn router(global: &crate::Global) -> Router { async fn post_package_archive( State(global): State, Path(repo): Path, - body: BodyStream, -) -> Result<(), StatusCode> { + mut body: BodyStream, +) -> crate::Result<()> { // let mut body_reader = tokio_util::io::StreamReader::new( // body.map_err(|err| io::Error::new(io::ErrorKind::Other, err)), // ); - let mut body = body.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR); // We first stream the uploaded file to disk let uuid: uuid::fmt::Simple = Uuid::new_v4().into(); let path = global.config.pkg_dir.join(uuid.to_string()); - let mut f = fs::File::create(&path) - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR) - .await?; + let mut f = fs::File::create(&path).await?; while let Some(chunk) = body.next().await { - f.write_all(&chunk?) - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR) - .await?; + f.write_all(&chunk?).await?; } let clone = Arc::clone(&global.repo_manager); tokio::task::spawn_blocking(move || clone.write().unwrap().add_pkg_from_path(&repo, &path)) - .map_err(|err| { - println!("{}", err); - StatusCode::INTERNAL_SERVER_ERROR - }) - .await? - .map_err(|err| { - println!("{}", err); - StatusCode::INTERNAL_SERVER_ERROR - }) + .await + .map_err(io::Error::from)??; + + Ok(()) }