dynerr
Some macros to help with dynamic error handling/logging.
The goal of this crate is to unify all error types without compromising type safety.
The main features of this crate are the dynerr!
and dynmatch!
macros. when used alongside the return type DynResult<T>
, they allows you to return multiple error types from a function then easily match for them during your error handling. Using dynerr, theres no need to ever wrap errors.
DynError
is an alias for Box<dyn error::Error>
. Any error that implements error::Error
can be turned into a DynError
.
DynResult<T>
is just an alias for Result<T, DynError>
so anything that works on a Result<T>
will still work on a DynResult<T>
.
Dynerr works with any error type from any crate, as long as the type being returned implements std::error::Error
then DynResult<T>
should be able to handle it.
To directly return a custom error its recommended to use the dynerr!
macro instead of Err()
.
To match against the DynError contained in DynResult<T>
, use the dynmatch!
macro.
dynmatch!
usage looks similar to this:
let i = match example ;
Aside from its main features, dynerr also has some simple macros to help with lazy logging.
log!
will log an event to the supplied file. Defaults to event.log if no log file supplied.
logged_panic!
will log an event to file then panic. Defaults to event.log if no log file supplied.
check!
will call .unwrap_or_else(|e| logged_panic!(e))
on a result. Defaults to event.log if no log file supplied.
If the supplied file doesn't exist then these macros will attempt to create the file.
To delete a log file use the clean!
macro.
These macros all rely on either the log
or clean_log
functions. these functions are capable of panicking but shouldn't ever need to under normal circumstances.
A complete example:
use *;
use ;
//THIS SECTION IS CREATING THE FIRST CUSTOM ERROR
///a custom error type
//impl display formatting for error
//impl error conversion for error
//THIS SECTION IS CREATING THE SECOND CUSTOM ERROR
///THIS SECTION IS USING IT
///shows error handling capabilities using DynError