diff options
author | boasting-squirrel <boasting.squirrel@gmail.com> | 2019-03-12 19:16:45 +0000 |
---|---|---|
committer | boasting-squirrel <boasting.squirrel@gmail.com> | 2019-03-12 19:16:45 +0000 |
commit | 122a949ec49f84a49e7a5bec657a93a65faadce1 (patch) | |
tree | 1bf07eabea70fa767db1d79a002070c286bb1861 /src | |
parent | Added some error messages + reworked the print_error_chain method (diff) | |
download | miniserve-122a949ec49f84a49e7a5bec657a93a65faadce1.tar.gz miniserve-122a949ec49f84a49e7a5bec657a93a65faadce1.zip |
Better error messages for invalid path
Diffstat (limited to 'src')
-rw-r--r-- | src/archive.rs | 16 | ||||
-rw-r--r-- | src/errors.rs | 14 | ||||
-rw-r--r-- | src/main.rs | 3 |
3 files changed, 20 insertions, 13 deletions
diff --git a/src/archive.rs b/src/archive.rs index 556fd69..9df1e5e 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -61,7 +61,21 @@ pub fn create_archive_file( /// Compresses a given folder in .tar.gz format fn tgz_compress(dir: &PathBuf) -> Result<(String, Bytes), errors::CompressionError> { let src_dir = dir.display().to_string(); - let inner_folder = dir.file_name()?.to_str()?; + let inner_folder = match dir.file_name() { + Some(directory_name) => match directory_name.to_str() { + Some(directory) => directory, + None => { + return Err(errors::CompressionError::new( + errors::CompressionErrorKind::InvalidUTF8DirectoryName, + )) + } + }, + None => { + return Err(errors::CompressionError::new( + errors::CompressionErrorKind::InvalidDirectoryName, + )) + } + }; let dst_filename = format!("{}.tar", inner_folder); let dst_tgz_filename = format!("{}.gz", dst_filename); diff --git a/src/errors.rs b/src/errors.rs index 191d382..6781bc6 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -11,10 +11,10 @@ pub enum CompressionErrorKind { CreateTemporaryFileError, #[fail(display = "Could not create file {}", path)] CreateFileError { path: String }, - #[fail(display = "Could not retrieve entity name from the given path. - This can either mean that the entity has non UTF-8 characters in its name, - or that its name ends with \"..\"")] + #[fail(display = "Invalid path: directory name cannot end with \"..\"")] InvalidDirectoryName, + #[fail(display = "Directory name contains invalid UTF-8 characters")] + InvalidUTF8DirectoryName, #[fail(display = "Failed to create the TAR archive: {}", message)] TarBuildingError { message: String }, #[fail(display = "Failed to create the GZIP archive: {}", message)] @@ -56,7 +56,7 @@ pub struct CompressionError { } impl CompressionError { - fn new(kind: CompressionErrorKind) -> CompressionError { + pub fn new(kind: CompressionErrorKind) -> CompressionError { CompressionError { inner: Context::new(kind), } @@ -98,9 +98,3 @@ impl From<CompressionErrorKind> for CompressionError { } } } - -impl From<std::option::NoneError> for CompressionError { - fn from(_: std::option::NoneError) -> CompressionError { - CompressionError::new(CompressionErrorKind::InvalidDirectoryName) - } -} diff --git a/src/main.rs b/src/main.rs index 59389b1..260551c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ #![feature(proc_macro_hygiene)] -#![feature(try_trait)] use actix_web::{fs, middleware, server, App}; use clap::crate_version; @@ -13,9 +12,9 @@ use yansi::{Color, Paint}; mod archive; mod args; mod auth; +mod errors; mod listing; mod renderer; -mod errors; #[derive(Clone, Debug)] /// Configuration of the Miniserve application |