From 720aae220142ff9bea1bde0f843d620d89a6cc70 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 29 Nov 2020 19:19:59 +0000 Subject: [PATCH] :recycle: Working on errors for file server and server --- crates/ptth_server/src/bin/ptth_file_server.rs | 7 ++++--- crates/ptth_server/src/errors.rs | 10 ++++++++++ crates/ptth_server/src/file_server/errors.rs | 4 ++-- crates/ptth_server/src/file_server/mod.rs | 17 +++++++++-------- crates/ptth_server/src/lib.rs | 8 +++++--- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/crates/ptth_server/src/bin/ptth_file_server.rs b/crates/ptth_server/src/bin/ptth_file_server.rs index ae5eaf3..aefba3a 100644 --- a/crates/ptth_server/src/bin/ptth_file_server.rs +++ b/crates/ptth_server/src/bin/ptth_file_server.rs @@ -23,6 +23,7 @@ use ptth_core::{ prelude::*, }; use ptth_server::{ + errors::ServerError, file_server, load_toml, }; @@ -46,7 +47,7 @@ fn status_reply > (status: StatusCode, b: B) } async fn handle_all (req: Request , state: Arc >) --> Result , hyper::http::Error> +-> Result , ServerError> { use std::str::FromStr; @@ -76,7 +77,7 @@ async fn handle_all (req: Request , state: Arc >) &ptth_req.uri, &ptth_req.headers, state.hidden_path.as_deref () - ).await; + ).await?; let mut resp = Response::builder () .status (StatusCode::from (ptth_resp.parts.status_code)); @@ -90,7 +91,7 @@ async fn handle_all (req: Request , state: Arc >) .unwrap_or_else (Body::empty) ; - resp.body (body) + Ok (resp.body (body)?) } #[derive (Deserialize)] diff --git a/crates/ptth_server/src/errors.rs b/crates/ptth_server/src/errors.rs index 25d8acc..5708af7 100644 --- a/crates/ptth_server/src/errors.rs +++ b/crates/ptth_server/src/errors.rs @@ -2,5 +2,15 @@ use thiserror::Error; #[derive (Debug, Error)] pub enum ServerError { + #[error ("File server error")] + FileServer (#[from] super::file_server::errors::FileServerError), + #[error ("Hyper HTTP error")] + Http (#[from] hyper::http::Error), + + #[error ("Hyper invalid header name")] + InvalidHeaderName (#[from] hyper::header::InvalidHeaderName), + + #[error ("Can't parse wrapped requests")] + CantParseWrappedRequests (rmp_serde::decode::Error), } diff --git a/crates/ptth_server/src/file_server/errors.rs b/crates/ptth_server/src/file_server/errors.rs index 3cc5808..de32bf9 100644 --- a/crates/ptth_server/src/file_server/errors.rs +++ b/crates/ptth_server/src/file_server/errors.rs @@ -2,6 +2,6 @@ use thiserror::Error; #[derive (Debug, Error)] pub enum FileServerError { - + #[error ("Handlebars render error")] + Handlebars (#[from] handlebars::RenderError), } - diff --git a/crates/ptth_server/src/file_server/mod.rs b/crates/ptth_server/src/file_server/mod.rs index 3715e2a..38dcfcf 100644 --- a/crates/ptth_server/src/file_server/mod.rs +++ b/crates/ptth_server/src/file_server/mod.rs @@ -51,7 +51,8 @@ use ptth_core::{ prefix_match, }; -mod errors; +pub mod errors; +use errors::FileServerError; #[derive (Debug, Serialize)] pub struct ServerInfo { @@ -239,11 +240,11 @@ async fn read_dir_entry (entry: DirEntry) -> TemplateDirEntry async fn serve_root ( handlebars: &Handlebars <'static>, server_info: &ServerInfo -) -> Response +) -> Result { - let s = handlebars.render ("file_server_root", &server_info).unwrap (); + let s = handlebars.render ("file_server_root", &server_info)?; - serve_html (s) + Ok (serve_html (s)) } fn serve_html (s: String) -> Response { @@ -615,11 +616,11 @@ pub async fn serve_all ( headers: &HashMap >, hidden_path: Option <&Path> ) - -> Response +-> Result { use InternalResponse::*; - match internal_serve_all (root, method, uri, headers, hidden_path).await { + Ok (match internal_serve_all (root, method, uri, headers, hidden_path).await { Favicon => serve_error (StatusCode::NotFound, ""), Forbidden => serve_error (StatusCode::Forbidden, "403 Forbidden"), InvalidUri => serve_error (StatusCode::BadRequest, "Invalid URI"), @@ -634,7 +635,7 @@ pub async fn serve_all ( }, Redirect (location) => serve_307 (location), - Root => serve_root (handlebars, server_info).await, + Root => serve_root (handlebars, server_info).await?, ServeDir (ServeDirParams { path, dir, @@ -651,7 +652,7 @@ pub async fn serve_all ( MarkdownError::NotUtf8 => serve_error (StatusCode::BadRequest, "File is not UTF-8"), }, MarkdownPreview (s) => serve_html (s), - } + }) } pub fn load_templates ( diff --git a/crates/ptth_server/src/lib.rs b/crates/ptth_server/src/lib.rs index 588a61e..d764a75 100644 --- a/crates/ptth_server/src/lib.rs +++ b/crates/ptth_server/src/lib.rs @@ -35,6 +35,8 @@ pub mod errors; pub mod file_server; pub mod load_toml; +use errors::ServerError; + // Thanks to https://github.com/robsheldon/bad-passwords-index const BAD_PASSWORDS: &[u8] = include_bytes! ("bad_passwords.txt"); @@ -61,7 +63,7 @@ struct ServerState { async fn handle_req_resp <'a> ( state: &Arc , req_resp: reqwest::Response -) -> Result <(), ()> { +) -> Result <(), ServerError> { //println! ("Step 1"); let body = req_resp.bytes ().await.unwrap (); @@ -70,7 +72,7 @@ async fn handle_req_resp <'a> ( Ok (x) => x, Err (e) => { error! ("Can't parse wrapped requests: {:?}", e); - return Err (()); + return Err (ServerError::CantParseWrappedRequests (e)); }, }; @@ -97,7 +99,7 @@ async fn handle_req_resp <'a> ( &parts.uri, &parts.headers, state.hidden_path.as_deref () - ).await; + ).await.unwrap (); let mut resp_req = state.client .post (&format! ("{}/http_response/{}", state.config.relay_url, req_id))