Skip to content

Commit

Permalink
feat: Improve new error display output
Browse files Browse the repository at this point in the history
Signed-off-by: robot9001 <robo9k@symlink.io>
  • Loading branch information
robo9k committed Sep 29, 2023
1 parent 3e3d0f8 commit bec91ae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
14 changes: 2 additions & 12 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,11 @@ pub(crate) fn setflags(cookie: libmagic::magic_t, flags: libc::c_int) -> Result<
}

#[derive(thiserror::Error, Debug)]
#[error("could not set magic cookie flags")]
#[error("could not set magic cookie flags {}", .flags)]
pub(crate) struct SetFlagsError {
flags: libc::c_int,
}

impl SetFlagsError {
pub fn flags(&self) -> libc::c_int {
self.flags
}
}

/// # Panics
///
/// Panics if `libmagic` violates its API contract, e.g. by not setting the last error or returning undefined data.
Expand Down Expand Up @@ -269,17 +263,13 @@ pub(crate) fn open(flags: libc::c_int) -> Result<libmagic::magic_t, OpenError> {
}

#[derive(thiserror::Error, Debug)]
#[error("could not open magic cookie")]
#[error("could not open magic cookie with flags {}: {}", .flags, .errno)]
pub(crate) struct OpenError {
flags: libc::c_int,
errno: std::io::Error,
}

impl OpenError {
pub fn flags(&self) -> libc::c_int {
self.flags
}

pub fn errno(&self) -> &std::io::Error {
&self.errno
}
Expand Down
41 changes: 29 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ bitflags::bitflags! {
}
}

impl std::fmt::Display for CookieFlags {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
bitflags::parser::to_writer(self, f)
}
}

fn db_filenames<P: AsRef<Path>>(filenames: &[P]) -> Result<Option<CString>, CookieDatabaseError> {
match filenames.len() {
0 => Ok(None),
Expand All @@ -310,32 +316,38 @@ fn db_filenames<P: AsRef<Path>>(filenames: &[P]) -> Result<Option<CString>, Cook
)
.map_err(|_| CookieDatabaseError {
kind: CookieDatabaseErrorKind::InvalidDatabaseFilePath,
source: None,
})?,
)),
}
}

/// Error within several [`Cookie`] database functions
#[derive(thiserror::Error, Debug)]
#[error("magic cookie database error")]
#[error("magic cookie database error: {}",
match .kind {
CookieDatabaseErrorKind::Libmagic { function, .. } => format!("in `libmagic` function {}", function),
CookieDatabaseErrorKind::InvalidDatabaseFilePath => "invalid database files path".to_string(),
}
)]
pub struct CookieDatabaseError {
kind: CookieDatabaseErrorKind,
//#[backtrace]
source: Option<crate::ffi::CookieError>,
}

#[derive(Debug)]
enum CookieDatabaseErrorKind {
Libmagic {
function: &'static str,
source: crate::ffi::CookieError,
},
Libmagic { function: &'static str },
InvalidDatabaseFilePath,
}

/// Error within several [`Cookie`] functions
#[derive(thiserror::Error, Debug)]
#[error("magic cookie error")]
#[error("magic cookie error in `libmagic` function {}", .function)]
pub struct CookieError {
function: &'static str,
//#[backtrace]
source: crate::ffi::CookieError,
}

Expand Down Expand Up @@ -421,8 +433,8 @@ impl Cookie {
Err(err) => Err(CookieDatabaseError {
kind: CookieDatabaseErrorKind::Libmagic {
function: "magic_check",
source: err,
},
source: Some(err),
}),
Ok(_) => Ok(()),
}
Expand All @@ -443,8 +455,8 @@ impl Cookie {
Err(err) => Err(CookieDatabaseError {
kind: CookieDatabaseErrorKind::Libmagic {
function: "magic_check",
source: err,
},
source: Some(err),
}),
Ok(_) => Ok(()),
}
Expand All @@ -463,8 +475,8 @@ impl Cookie {
Err(err) => Err(CookieDatabaseError {
kind: CookieDatabaseErrorKind::Libmagic {
function: "magic_list",
source: err,
},
source: Some(err),
}),
Ok(_) => Ok(()),
}
Expand Down Expand Up @@ -501,8 +513,8 @@ impl Cookie {
Err(err) => Err(CookieDatabaseError {
kind: CookieDatabaseErrorKind::Libmagic {
function: "magic_load",
source: err,
},
source: Some(err),
}),
Ok(_) => Ok(()),
}
Expand Down Expand Up @@ -553,7 +565,12 @@ impl Cookie {

/// Error within [`Cookie::open`](Cookie::open)
#[derive(thiserror::Error, Debug)]
#[error("could not open magic cookie")]
#[error("could not open magic cookie: {}",
match .kind {
CookieOpenErrorKind::UnsupportedFlags => format!("unsupported flags {}", .flags),
CookieOpenErrorKind::Errno => "other error".to_string(),
}
)]
pub struct CookieOpenError {
flags: CookieFlags,
kind: CookieOpenErrorKind,
Expand All @@ -572,7 +589,7 @@ enum CookieOpenErrorKind {

/// Error within [`Cookie::set_flags`](Cookie::set_flags)
#[derive(thiserror::Error, Debug)]
#[error("could not set magic cookie flags")]
#[error("could not set magic cookie flags {}", .flags)]
pub struct CookieSetFlagsError {
flags: CookieFlags,
//#[backtrace]
Expand Down

0 comments on commit bec91ae

Please sign in to comment.