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
#![feature(backtrace)]

mod as_error;
mod exception;
mod context;

#[doc(inline)]
/// Annotations a function that "throws" a Result.
///
/// Inside functions tagged with `throws`, you can use `?` and the `throw!` macro to return errors,
/// but you don't need to wrap the successful return values in `Ok`.
///
/// `throws` can optionally take a type as an argument, which will be the error type returned by
/// this function. By default, the function will throw `Exception`.
pub use fehler_macros::throws;

#[doc(inline)]
/// Derive for the Error trait.
///
/// If the type contains a `Backtrace`, it will generate the `backtrace` method to find it.
///
/// If this wraps a different, underlying error, you can tag that field with `#[error::source]`,
/// which will generate the `cause` and `source` methods correctly.
pub use fehler_macros::Error;

pub use crate::as_error::AsError;
pub use crate::exception::{Exception, Errors};
pub use crate::context::Context;

/// Throw an error.
///
/// This macro is equivalent to `Err($err)?`.
#[macro_export]
macro_rules! throw {
    ($err:expr)   => (return std::result::Result::Err(std::convert::From::from($err)))
}

/// Construct an ad-hoc exception from a string.
///
/// This evaluates to an `Exception`. 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`.
#[macro_export]
macro_rules! error {
    ($e:expr)   => { $crate::Exception::new_adhoc($e) };
    ($($arg:tt)*) => { $crate::Exception::new_adhoc(format!($($arg)*)) };
}