I like file and line numbers with my errors:
// No need to use this directly. See the e!() macro.
#[derive(Debug)]
pub struct ErrInfo {
pub file: &'static str,
pub line: u32,
pub msg: &'static str
}
impl std::fmt::Display for ErrInfo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if cfg!(debug_assertions) {
write!(f, "[{}:{}] {}", self.file, self.line, self.msg)
} else {
write!(f, "{}", self.msg)
}
}
}
/// Use this with `failure` crate's `ResultExt` like this
///
/// ```
/// $r = function().context(e!("Function failed"))?;
/// ```
///
/// Error context will then include that message, and the file and line number.
#[macro_export]
macro_rules! e {
($msg:expr) => {
$crate::ErrInfo {
file: file!(),
line: line!(),
msg: $msg
}
};
}