Skip to content
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

Use thiserror consistently across the crate #62

Closed
plafer opened this issue Nov 8, 2023 · 5 comments · Fixed by #207
Closed

Use thiserror consistently across the crate #62

plafer opened this issue Nov 8, 2023 · 5 comments · Fixed by #207
Assignees
Milestone

Comments

@plafer
Copy link
Contributor

plafer commented Nov 8, 2023

From #55 (comment).

I advocate that we use thiserror for errors across the repo.

Pros:

  • Implements std::error::Error for you
  • Implements Display for you (with the attribute #[error("display string for error variant")]
  • Doesn't appear in the public API, so we can stop using it at any point without breaking changes
  • Convenience attributes, such as #[from] which implements a From for the variant that it's defined on (which plays nicely with the ? operator)
  • Widely used, and well maintained (by dtolnay)

Cons:

  • Doesn't work with no_std, only due to the fact that std::error::Error lives in std instead of core, but this is eventually going to change

Alternatives:

  • We could go with anyhow or eyre instead if we want to write "one-off errors" (no enums defined) everywhere and lose the ability to match on specific error variants. We can also use both: thiserror for all library code, and anyhow in the binary or anywhere we're absolutely certain the error is only meant to be printed out to the user
@bobbinth
Copy link
Contributor

bobbinth commented Nov 8, 2023

For the node, I don't think we are aiming for no_std support - so, I think using thiserror is fine.

@bitwalker
Copy link

bitwalker commented Nov 14, 2023

We use both anyhow and thiserror in the compiler, leaning more on custom error types via thiserror than anyhow, but I have nothing but good things to say about both projects.

Biggest downside of thiserror is definitely the fact that the Error trait is only in libstd, but once that changes it'll be borderline essential to use, since otherwise the boilerplate for error types is nuts.

@hackaugusto
Copy link
Contributor

Just to clarify, the goal here is to use thiserror in addition to anyhow, correct?

@bobbinth
Copy link
Contributor

Not sure of pros and cons - but seems like we should mostly lean on thiserror and use anyhow only in binaries?

@bitwalker
Copy link

I would only use anyhow in functions which join together different error types from the same crate. If you find yourself writing error types with multiple "#[error(transparent)]" variants, that's a sign anyhow::Error is a better fit there. Within a crate, it can still be useful to keep strongly-typed errors so that you can handle those errors. Once something is an anyhow::Error, it's a lot more awkward to handle errors directly, and more likely that they just get yeeted up the stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants