Mapping from one context to another #8481
-
|
I have a codebase where errors are stacked 😅 in different modules: Now, I would like to create the higher level JobError depending on lower level DBError and map all or some of the lower lever errs to related ones on higher level. Is there a way to add a context with access to the lower level context that can be function chained line .change_context()? I know I can match on the intermediate Ideally, something like N.b. Don't take this too seriously: I think |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Hey @chrs-b, thanks for bringing this up!
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the quick reply! In my example I need some (not all) of the underlying errs mapped to higher level and several lower errors might result in a higher one. I can see your point, but it would require to "shadow" all lower errors with higher level ones. For my use case it would be great to chain it up, as the match and map is quite lengthy. Anyways, thanks for your insight, will code around it. Overall a great crate, I use it everywhere in my new project and error msgs are a dream come true. Thanks for sharing all your hard work! |
Beta Was this translation helpful? Give feedback.
Hey @chrs-b, thanks for bringing this up!
change_mapped_contextI can see the appeal, but I think this would actually go against a design choice in error-stack that's worth explaining.
change_contextis designed so the new context comes from what the caller knows, not from what the lower error contains. The job layer knows "I was processing a job", the database layer knows "a query failed" — these are independent facts:Deriving
JobErrorfromDBErrorvia something likechange_mapped_context(|ctx| JobError::from(ctx))is really a re-categorization, not adding context. IfJobErrorcan be mechanically derived fromDBError…