Show better error messages

This commit is contained in:
Vinícius Rodrigues Miguel 2021-03-30 00:11:46 -03:00
parent 2dc828c0ff
commit b002de78d9
6 changed files with 21 additions and 19 deletions

View File

@ -179,7 +179,6 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
output: Some(File { output: Some(File {
path: output_file.into(), path: output_file.into(),
contents_in_memory: None, contents_in_memory: None,
// extension: output_file_extension.ok(),
extension: Some(output_file_extension.unwrap()), extension: Some(output_file_extension.unwrap()),
}), }),
}) })

View File

@ -36,8 +36,7 @@ impl BzipCompressor {
let bytes = match file.contents_in_memory { let bytes = match file.contents_in_memory {
Some(bytes) => bytes, Some(bytes) => bytes,
None => { None => {
// TODO: error message, return Err(crate::Error::InternalError);
return Err(crate::Error::InvalidInput);
} }
}; };

View File

@ -14,8 +14,8 @@ impl TarCompressor {
// TODO: implement this // TODO: implement this
fn make_archive_from_memory(_input: File) -> crate::Result<Vec<u8>> { fn make_archive_from_memory(_input: File) -> crate::Result<Vec<u8>> {
println!( println!(
"{}: .tar.tar and .zip.tar is currently unimplemented.", "{} .tar.tar and .zip.tar is currently unimplemented.",
"error".red() "[ERROR]".red()
); );
Err(crate::Error::InvalidZipArchive("")) Err(crate::Error::InvalidZipArchive(""))
} }

View File

@ -18,6 +18,7 @@ pub enum Error {
InputsMustHaveBeenDecompressible(PathBuf), InputsMustHaveBeenDecompressible(PathBuf),
InternalError, InternalError,
CompressingRootFolder, CompressingRootFolder,
WalkdirError
} }
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
@ -38,25 +39,33 @@ impl fmt::Debug for Error {
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} ", "[ERROR]".red())?;
match self { match self {
Error::MissingExtensionError(filename) => { Error::MissingExtensionError(filename) => {
write!(f, "{} ", "[ERROR]".red())?;
write!(f, "cannot compress to \'{}\', likely because it has an unsupported (or missing) extension.", filename) write!(f, "cannot compress to \'{}\', likely because it has an unsupported (or missing) extension.", filename)
} }
Error::InputsMustHaveBeenDecompressible(file) => { Error::InputsMustHaveBeenDecompressible(file) => {
write!(f, "{} ", "[ERROR]".red())?;
write!(f, "file '{:?}' is not decompressible", file) write!(f, "file '{:?}' is not decompressible", file)
} }
Error::WalkdirError => {
// Already printed in the From block
write!(f, "")
}
Error::FileNotFound(file) => { Error::FileNotFound(file) => {
write!(f, "{} ", "[ERROR]".red())?;
// TODO: check if file == "" // TODO: check if file == ""
write!(f, "file {:?} not found!", file) write!(f, "file {:?} not found!", file)
} }
Error::CompressingRootFolder => { Error::CompressingRootFolder => {
write!(f, "{} ", "[ERROR]".red())?;
let spacing = " "; let spacing = " ";
writeln!(f, "It seems you're trying to compress the root folder.")?; writeln!(f, "It seems you're trying to compress the root folder.")?;
writeln!(f, "{}This is unadvisable since ouch does compressions in-memory.", spacing)?; writeln!(f, "{}This is unadvisable since ouch does compressions in-memory.", spacing)?;
write!(f, "{}Use a more appropriate tool for this, such as {}.", spacing, "rsync".green()) write!(f, "{}Use a more appropriate tool for this, such as {}.", spacing, "rsync".green())
} }
Error::InternalError => { Error::InternalError => {
write!(f, "{} ", "[ERROR]".red())?;
write!(f, "You've reached an internal error! This really should not have happened.\nPlease file an issue at {}", "https://github.com/vrmiguel/ouch".green()) write!(f, "You've reached an internal error! This really should not have happened.\nPlease file an issue at {}", "https://github.com/vrmiguel/ouch".green())
} }
_err => { _err => {
@ -74,7 +83,7 @@ impl From<std::io::Error> for Error {
std::io::ErrorKind::PermissionDenied => Self::PermissionDenied, std::io::ErrorKind::PermissionDenied => Self::PermissionDenied,
std::io::ErrorKind::AlreadyExists => Self::AlreadyExists, std::io::ErrorKind::AlreadyExists => Self::AlreadyExists,
_other => { _other => {
println!("{}: {}", "IO error".red(), err); println!("{} {}", "[IO error]".red(), err);
Self::IoError Self::IoError
} }
} }
@ -95,7 +104,7 @@ impl From<zip::result::ZipError> for Error {
impl From<walkdir::Error> for Error { impl From<walkdir::Error> for Error {
fn from(err: walkdir::Error) -> Self { fn from(err: walkdir::Error) -> Self {
eprintln!("{}: {}", "error".red(), err); eprintln!("{} {}", "[ERROR]".red(), err);
Self::InvalidInput Self::WalkdirError
} }
} }

View File

@ -29,10 +29,10 @@ impl Evaluator {
None => { None => {
// This block *should* be unreachable // This block *should* be unreachable
eprintln!( eprintln!(
"{}: reached Evaluator::get_decompressor without known extension.", "{} reached Evaluator::get_decompressor without known extension.",
"internal error".red() "[internal error]".red()
); );
return Err(crate::Error::InvalidInput); return Err(crate::Error::InternalError);
} }
}; };
@ -71,8 +71,8 @@ impl Evaluator {
None => { None => {
// This block *should* be unreachable // This block *should* be unreachable
eprintln!( eprintln!(
"{}: reached Evaluator::get_decompressor without known extension.", "{} reached Evaluator::get_decompressor without known extension.",
"internal error".red() "[internal error]".red()
); );
return Err(crate::Error::InvalidInput); return Err(crate::Error::InvalidInput);
} }

View File

@ -13,11 +13,6 @@ where
{ {
let exists = path.as_ref().exists(); let exists = path.as_ref().exists();
if !exists { if !exists {
eprintln!(
"{}: could not find file {:?}",
"[ERROR]".red(),
path.as_ref()
);
return Err(crate::Error::FileNotFound(PathBuf::from(path.as_ref()))); return Err(crate::Error::FileNotFound(PathBuf::from(path.as_ref())));
} }
Ok(()) Ok(())