1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/// Construct an ad-hoc error from a string or existing non-`narrate` error
/// value.
///
/// This evaluates to an [`Error`][crate::Error]. It can take either just a
/// string, or a format string with arguments. It also can take any custom type
/// which implements `Debug` and `Display`.
///
/// If called with a single argument whose type implements `std::error::Error`
/// (in addition to `Debug` and `Display`, which are always required), then that
/// Error impl's `source` is preserved as the `source` of the resulting
/// `narrate::Error`.
///
/// # Example
///
/// ```
/// # type V = ();
/// #
/// use narrate::{error_from, Result};
///
/// fn lookup(key: &str) -> Result<V> {
/// if key.len() != 16 {
/// return Err(error_from!("key length must be 16 characters, got {:?}", key));
/// }
///
/// // ...
/// # Ok(())
/// }
/// ```
#[macro_export]
macro_rules! error_from {
($msg:literal $(,)?) => {
$crate::Error::from_anyhow(anyhow::anyhow!($msg))
};
($err:expr $(,)?) => {
$crate::Error::from_anyhow(anyhow::anyhow!($err))
};
($fmt:expr, $($arg:tt)*) => {
$crate::Error::from_anyhow(anyhow::anyhow!($fmt, $($arg)*))
};
}
/// Return early with an error.
///
/// This macro is equivalent to `return Err(`[`error_from!($args...)`][error_from!]`)`.
///
/// The surrounding function's or closure's return value is required to be
/// `Result<_,`[`narrate::Error`][crate::Error]`>`.
///
/// [error_from!]: crate::error_from
///
/// # Example
///
/// ```
/// # use narrate::{bail, Result};
/// #
/// # fn has_permission(user: usize, resource: usize) -> bool {
/// # true
/// # }
/// #
/// # fn main() -> Result<()> {
/// # let user = 0;
/// # let resource = 0;
/// #
/// if !has_permission(user, resource) {
/// bail!("permission denied for accessing {}", resource);
/// }
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! bail {
($msg:literal $(,)?) => {
return ::core::result::Result::Err($crate::error_from!($msg))
};
($err:expr $(,)?) => {
return ::core::result::Result::Err($crate::error_from!($err))
};
($fmt:expr, $($arg:tt)*) => {
return ::core::result::Result::Err($crate::error_from!($fmt, $($arg)*))
};
}