-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error handling #64
Comments
The best is actually to just use anyhow, see withoutboats blog. In any case, why did you remove |
The problem is the |
having a struct for each error and |
The recommendation from withoutboats is about applications, not about libraries:
From https://boats.gitlab.io/blog/post/failure-to-fehler/ I would strongly suggest not depending on Unfortunately |
I believe that I've seen him say otherwise too, although I'm having difficulty finding a quote. So there are two types of errors, errors that need to be handled by the user and errors that are fatal. In most cases if an error happens it is propagated all the way to main after which it is displayed. One way to make sure that errors that need to be handled aren't accidentally propagated is using a nested |
Note that often things that need to be handled by the user use an |
Another problem is I actually do want to handle an error, for example a io error of some sort. A few layers up, it is really hard to get the io error: match error {
libipld::Error::Cid(cid::Error::Multihash(multihash::Errror::Io(io))) if io.kind() == ErrorKind::WouldBlock => {}
libipld::Error::Multihash(multihash::Error::Io(io))) if io.kind() ... => {}
libipld::Error::Io(io) if ... => {}
libipld::Error::Cbor(cbor::Error::Io(io)) if ... => {}
} This is a nuissance. With the method I proposed you can handle it easily: if let Some(error) = error.downcast_ref(io::Error) {
...
} |
Put a different way, what ways are there to handle an error?
Now in all the rust code you've written, how many errors where of the case 3 or 4? 3 is most of the time a |
The current error handling provides poor reporting. For example any error within Multihash or Multibase just lead to an
Error::ParsingError
without much further information.I propose using https://crates.io/crates/thiserror, so that we can nest the actual underlying error.
The text was updated successfully, but these errors were encountered: