error-stack: Anyhow like simple error propagation #3311
Replies: 2 comments
-
Hi @fominv! Thank you for the question. I sympathize with your want to sometimes not create a new error or a new error layer, but allowing for something as you described would go against the spirit of Sure, at the moment, this might seem like a good idea, as one might think: "This is temporary, I will add more details later," but from my experience, this won't happen most of the time, and a once temporary stopgap becomes a permanent fixture, which then leads to confusion in published libraries or displayed errors. You can use something like an If you find yourself struggling to find a name for your variant, you might want to consider instead moving to a struct like: #[derive(Error, Debug)]
struct ErrorB; This would circumvent the naming issue; if you still want names for your other variants, you can add an intermediary context for those that is a proper enum!
|
Beta Was this translation helpful? Give feedback.
-
Thank you for the quick reply! Will try with the ideas you proposed. |
Beta Was this translation helpful? Give feedback.
-
Hi All,
I have been trying out error_stack and I quite like the idea of error layers throughout a rust program. However, sometimes I wish for nicer ergonomics where at the moment I do not want to define a custom type for an error:
Let's say that foo does some stuff and returns an error which at the moment, I do not really care about. In that case, I would like to call something like
anyhow!().attach_printable("meh")
and return.At the call site, I would like to be able to wrap any error type from a lower level error with
anyhow()
again (potentially add context or not) in case I do not really care about the error.The enum variant could also be derived with its own derive macro.
I think this improves the ergonomics of error_stack in cases where one still prototypes an application and does not know the exact error layers at the moment. If at some point one wants more fine grained control, then adding another enum variant and calling
.change_context(ErrorB::AnotherVariant)
would be simple.This solves my underlying problem of using other libraries / operations. When they return a result I am sometimes hard pressed of finding names and / or categorizing the error in this layer. I think in that case having a generic Anyhow error with nice ergonomics would make more sense than coming with a probably bad name on the spot.
Beta Was this translation helpful? Give feedback.
All reactions