GithubHelp home page GithubHelp logo

Comments (6)

tjkirch avatar tjkirch commented on July 27, 2024 1

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.

shepmaster avatar shepmaster commented on July 27, 2024 1

in other words, converting Result to Option and using SNAFU's OptionExt instead of ResultExt. .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.

HenningTimm avatar HenningTimm commented on July 27, 2024

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.

jamessewell avatar jamessewell commented on July 27, 2024

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.

HenningTimm avatar HenningTimm commented on July 27, 2024

Intentionally useless errors to avoid side channel attacks. Fascinating.

In light of this use case, consider my concerns addressed.

from snafu.

shepmaster avatar shepmaster commented on July 27, 2024

Some people may also be interested in #78.

from snafu.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.