♻️ Working on errors for file server and server

main
_ 2020-11-29 19:19:59 +00:00
parent c3ff3deb8e
commit 720aae2201
5 changed files with 30 additions and 16 deletions

View File

@ -23,6 +23,7 @@ use ptth_core::{
prelude::*,
};
use ptth_server::{
errors::ServerError,
file_server,
load_toml,
};
@ -46,7 +47,7 @@ fn status_reply <B: Into <Body>> (status: StatusCode, b: B)
}
async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
-> Result <Response <Body>, hyper::http::Error>
-> Result <Response <Body>, ServerError>
{
use std::str::FromStr;
@ -76,7 +77,7 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
&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 <Body>, state: Arc <ServerState <'static>>)
.unwrap_or_else (Body::empty)
;
resp.body (body)
Ok (resp.body (body)?)
}
#[derive (Deserialize)]

View File

@ -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),
}

View File

@ -2,6 +2,6 @@ use thiserror::Error;
#[derive (Debug, Error)]
pub enum FileServerError {
#[error ("Handlebars render error")]
Handlebars (#[from] handlebars::RenderError),
}

View File

@ -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 <Response, FileServerError>
{
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 <String, Vec <u8>>,
hidden_path: Option <&Path>
)
-> Response
-> Result <Response, FileServerError>
{
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 (

View File

@ -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 <ServerState>,
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))