Improve BevyError ergonomics#24528
Conversation
Add a `context` method to `Result` and `Option` and some extra utilities to `ResultSeverityExt`
|
It looks like your PR has been selected for a highlight in the next release blog post, but you didn't provide a release note. Please review the instructions for writing release notes, then expand or revise the content in the release notes directory to showcase your changes. |
|
Thank you for working on this! This should close #19714. |
SpecificProtagonist
left a comment
There was a problem hiding this comment.
Generally in favor of both the severity shorthands and the error context, though they could be done in separate PRs because they're independent. The fix to backtrace filtering also shouldn't be part of this PR.
| } | ||
| } | ||
|
|
||
| impl<T> ContextExt<T> for Option<T> { |
There was a problem hiding this comment.
This seems odd to me and might be a bit confusing to use. Attaching a context to an option turns the context into an error? And options work with .with_context but not with .with_severity (as map_severity can't work with options)?
There was a problem hiding this comment.
having an Option that's None usually means something failed, i.e. getting an element from an array, so you can attach a context message to it to turn it into an error that can be displayed. anyhow does the same thing so i just copied it from there really :p
having with_severity on an Option means that it'll need to turn into an error with some message, but if you then use context on it now you have 2 messages (i.e. some context message: option is none), which is different from if you first used context and then with_severity (you'll just get some context message)
Agreed, I would prefer this split into three :) |
Objective
Improve ergonomics when using
BevyErrorSolution
.info(),.warn(), etc. shorthand methods for.with_severitytoResultSeverityExt.context()extension method similar to the one fromanyhow, which adds a "context" message to go along with the error. Also works onOption<T>!Fixes #19714.
Testing
Added the
contextunit test to test messages produced byContextErrorand acontext_downcastingunit test to test that downcasting still works when using.context