diff options
author | boastful-squirrel <boastful.squirrel@gmail.com> | 2019-04-21 22:39:38 +0000 |
---|---|---|
committer | boastful-squirrel <boastful.squirrel@gmail.com> | 2019-04-21 22:39:38 +0000 |
commit | 6dad3eb1bf0cb3b36cdb3b312cca7caa91de2f57 (patch) | |
tree | fffc75935db79ef4b08c8502f5afe9ca9470867a /src | |
parent | Print upload/archive errors also in terminal (diff) | |
download | miniserve-6dad3eb1bf0cb3b36cdb3b312cca7caa91de2f57.tar.gz miniserve-6dad3eb1bf0cb3b36cdb3b312cca7caa91de2f57.zip |
Properly log error + added render_error method
Diffstat (limited to '')
-rw-r--r-- | src/errors.rs | 6 | ||||
-rw-r--r-- | src/file_upload.rs | 12 | ||||
-rw-r--r-- | src/listing.rs | 5 | ||||
-rw-r--r-- | src/renderer.rs | 2 |
4 files changed, 16 insertions, 9 deletions
diff --git a/src/errors.rs b/src/errors.rs index f2d185e..2bf4130 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -50,6 +50,12 @@ pub enum ContextualErrorKind { ArchiveCreationError(String, Box<ContextualError>), } +pub fn log_error_chain(description: String) { + for cause in description.lines() { + log::error!("{}", cause); + } +} + /// Based on https://boats.gitlab.io/failure/error-errorkind.html pub struct ContextualError { inner: Context<ContextualErrorKind>, diff --git a/src/file_upload.rs b/src/file_upload.rs index 54c56dd..3b84721 100644 --- a/src/file_upload.rs +++ b/src/file_upload.rs @@ -10,8 +10,8 @@ use std::{ path::{Component, PathBuf}, }; -use crate::errors::ContextualErrorKind; -use crate::renderer::file_upload_error; +use crate::errors::{self, ContextualErrorKind}; +use crate::renderer; /// Query parameters #[derive(Debug, Deserialize)] @@ -31,11 +31,11 @@ fn save_file( ))); } - let mut file = match std::fs::File::create(file_path) { + let mut file = match std::fs::File::create(&file_path) { Ok(file) => file, Err(e) => { return Box::new(future::err(ContextualErrorKind::IOError( - "Failed to create file".to_string(), + format!("Failed to create file in {}", file_path.display()), e, ))); } @@ -175,10 +175,10 @@ fn create_error_response( description: &str, return_path: &str, ) -> FutureResult<HttpResponse, actix_web::error::Error> { - log::error!("{}", description); + errors::log_error_chain(description.to_string()); future::ok( HttpResponse::BadRequest() .content_type("text/html; charset=utf-8") - .body(file_upload_error(description, return_path).into_string()), + .body(renderer::render_error(description, return_path).into_string()), ) } diff --git a/src/listing.rs b/src/listing.rs index 4473c6d..0ac02ff 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -12,6 +12,7 @@ use strum_macros::{Display, EnumString}; use crate::archive; use crate::renderer; use crate::themes; +use crate::errors; /// Query parameters #[derive(Deserialize)] @@ -262,10 +263,10 @@ pub fn directory_listing<S>( .body(Body::Streaming(Box::new(once(Ok(content)))))) } Err(err) => { - log::error!("{}", &err); + errors::log_error_chain(err.to_string()); Ok(HttpResponse::Ok() .status(http::StatusCode::INTERNAL_SERVER_ERROR) - .body(err.to_string())) + .body(renderer::render_error(&err.to_string(), serve_path).into_string()) } } } else { diff --git a/src/renderer.rs b/src/renderer.rs index be7a51f..eccc72d 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -761,7 +761,7 @@ fn humanize_systemtime(src_time: Option<SystemTime>) -> Option<String> { } /// Renders error page when file uploading fails -pub fn file_upload_error(error_description: &str, return_address: &str) -> Markup { +pub fn render_error(error_description: &str, return_address: &str) -> Markup { html! { pre { (error_description) } a href=(return_address) { |