From aeb51dcf43665741a3438360151a4424e9b243e0 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Tue, 12 Mar 2019 00:25:56 +0100 Subject: Started to add helpful messages for errors which could occur during archiving --- src/errors.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/errors.rs (limited to 'src/errors.rs') diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..c85d123 --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,90 @@ +use failure::{Backtrace, Context, Fail}; +use std::fmt::{self, Debug, Display}; +use yansi::Color; + +/// Kinds of error which might happen during folder archive generation +#[derive(Clone, Debug, PartialEq, Eq, Fail)] +pub enum CompressionErrorKind { + #[fail(display = "Could not open file {}", path)] + OpenFileError { path: String }, + #[fail(display = "Could not create temporary file")] + 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 \"..\"")] + InvalidDirectoryName, + #[fail(display = "Failed to create the TAR archive: {}", message)] + TarBuildingError { message: String }, + #[fail(display = "Failed to create the GZIP archive")] + GZipBuildingError, + #[fail(display = "Failed to retrieve TAR content")] + TarContentError, + #[fail(display = "Failed to retrieve GZIP content")] + GZipContentError, +} + +pub fn print_chain(err: CompressionError) { + for cause in Fail::iter_causes(&err) { + println!( + "{} {}", + Color::Magenta.paint("Caused by:").to_string(), + cause + ); + } +} + +pub struct CompressionError { + inner: Context, +} + +impl CompressionError { + fn new(kind: CompressionErrorKind) -> CompressionError { + CompressionError { + inner: Context::new(kind), + } + } +} + +impl Fail for CompressionError { + fn cause(&self) -> Option<&Fail> { + self.inner.cause() + } + + fn backtrace(&self) -> Option<&Backtrace> { + self.inner.backtrace() + } +} + +impl Display for CompressionError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + Display::fmt(&self.inner, f) + } +} + +impl Debug for CompressionError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + Debug::fmt(&self.inner, f) + } +} + +impl From> for CompressionError { + fn from(inner: Context) -> CompressionError { + CompressionError { inner } + } +} + +impl From for CompressionError { + fn from(kind: CompressionErrorKind) -> CompressionError { + CompressionError { + inner: Context::new(kind), + } + } +} + +impl From for CompressionError { + fn from(_: std::option::NoneError) -> CompressionError { + CompressionError::new(CompressionErrorKind::InvalidDirectoryName) + } +} -- cgit v1.2.3