Expand description
Error construction macros
These are specialized macros specific to this project’s patterns for throwing Errors; they make Error construction succinct and reduce clutter. They are developed from folding existing patterns into the macro while fixing several anti-patterns in the codebase.
- The primary macros
Err!anderr!are provided.Err!simply wrapserr!in the Result variant to reduceErr(err!(...))boilerplate, thuserr!can be used in any case.
-
The macro makes the general Error construction easy:
return Err!("something went wrong")replaces the priorreturn Err(Error::Err("something went wrong".to_owned())). -
The macro integrates format strings automatically:
return Err!("something bad: {msg}")replaces the priorreturn Err(Error::Err(format!("something bad: {msg}"))). -
The macro scopes variants of Error:
return Err!(Database("problem with bad database."))replaces the priorreturn Err(Error::Database("problem with bad database.")). -
The macro matches and scopes some special-case sub-variants, for example with ruma ErrorKind:
return Err!(Request(MissingToken("you must provide an access token"))). -
The macro fixes the anti-pattern of repeating messages in an error! log and then again in an Error construction, often slightly different due to the Error variant not supporting a format string. Instead
return Err(Database(error!("problem with db: {msg}")))logs the error at the callsite and then returns the error with the same string. Caller has the option of replacingerror!withdebug_error!.
Structs§
- Visitor 🔒