Comments (6)
I've also had times when I didn't want to expose the source error - because it would be confusing for the end user, or because I knew more than the source did about context and can actually provide better detail.
In those cases, I've used the pattern of source().ok().context(MyError)
- in other words, converting Result
to Option
and using SNAFU's OptionExt
instead of ResultExt
. .ok()
is nice and short, and requires you to explicitly throw away the source context, so I was happy with that. Maybe it's worth documenting?
from snafu.
in other words, converting
Result
toOption
and using SNAFU'sOptionExt
instead ofResultExt
..ok()
is nice and short,
Notably, it's shorter than my proposed function:
"42".parse::<i32>().ok().context(Parsing);
"42".parse::<i32>().lossy_context(Parsing);
It also reuses existing concepts (Result::ok
) instead of creating new SNAFU-specific concepts.
Perhaps this should just be documented in the cookbook.
from snafu.
I am not sure if this is a good idea. If I understood this correctly, this would allow me to easily replace an error with a snafu-error (like shown above), instead of doing something like this:
match "42".parse::<i32>() {
Ok(x) => Ok(x),
Err(_) => Err(Parsing),
}
Having the option to do this sounds nice.
However, I am not sure if this incentivizes good error handling. While I have not yet come across this use case, I could imagine to use this for merging several errors. This could make it too easy to just shadow underlying errors, for example to replace errors from several parsing functions with just one parsing error.
On the other hand, it is already possible to do this (see above), so making it more ergonomic might help people. A line in the documentation that this should be used carefully should be enough.
from snafu.
This is useful when the underlying error is intentionally useless.
Example:
https://briansmith.org/rustdoc/ring/error/struct.Unspecified.html
An error with absolutely no details.
ring uses this unit type as the error type in most of its results because (a) usually the specific reasons for a failure are obvious or are not useful to know, and/or (b) providing more details about a failure might provide a dangerous side channel, and/or (c) it greatly simplifies the error handling logic.
So in this case all you know is that it's a CryptoError - you don't want the source because it's not only useless, it's actually quite confusing to bubble up.
from snafu.
Intentionally useless errors to avoid side channel attacks. Fascinating.
In light of this use case, consider my concerns addressed.
from snafu.
Some people may also be interested in #78.
from snafu.
Related Issues (20)
- alternate `Display` to include the source chain HOT 4
- Best way to consolidate error types HOT 9
- Investigate switching `IntoError` from an associated type to generics HOT 2
- Investigate adding `ResultExt::boxed` and `::boxed_local`
- Is it possible to automatically include fields in error's display? HOT 2
- Consider adding #[snafu(transparent)] HOT 6
- Support Serde for the built-in types HOT 6
- multiple error types (struct and enum) and generic `IntoError` HOT 2
- Simple context usage, mysterious compile error HOT 2
- Use the same deprecation macros on the no_std Error as the official Error HOT 3
- Add an ensure macro that works with pattern matching HOT 2
- Restore support for yeeting context selectors HOT 1
- Print multi-line errors on their own lines in `Report` HOT 2
- Const generics seem to be giving the derive macro trouble HOT 2
- question: Best practice for automatic error conversion over two module levels?
- Cannot use a generic with a whatever variant
- Incorrect year in changelog
- No apparent way to have an error enum variant documentation generated from a display string HOT 5
- Why isn't Whatever Send + Sync by default? HOT 5
- `Report` print doesn't prefix with "Error: " HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from snafu.