errgo
Generate enum error variants inline.
A slightly type-safer take on anyhow, where each ad-hoc error is handleable by the caller. Designed to play nice with other crates like strum or thiserror.
This crate was written to aid wrapping C APIs - transforming e.g error codes to handleable messages.
It shouldn't really be used for library api entry points - a well-considered top-level error type is likely to be both more readable and forward compatible.
Consider reading Study of std::io::Error or simply making all generated structs pub(crate).
use errgo;
Under the hood, a struct like this is generated:
Note that the struct definition is placed just above the function body, meaning that you can't use [errgo] on functions in impl blocks - you'll have to move the function body to an outer scope, and call it in the impl block.
Importantly, you can derive on the generated struct, and passthrough attributes, allowing you to use crates like thiserror or strum.
See the [errgo] documentation for other arguments accepted by the macro.
Which generates the following:
And err! macro invocations are replaced with struct instantiations - no matter where they are in the function body!
If you need to reuse the same variant within a function, just use the normal construction syntax: