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

Replace thiserror with SNAFU #379

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

Conversation

jplatte
Copy link
Member

@jplatte jplatte commented Dec 29, 2024

If we decide to go this path, we should probably convert all of the crates.

This forces us to be much more explicit about error propagation and adding any relevant context, but makes that very convenient.

fs::write(&path, serialized).map_err(|io| SaveError::Write(path, io))?;
fs::write(&path, serialized).context(WriteSnafu)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the benefit of sanfu over thiserror? It seems like we're just exchanging map_err for a custom trait / .context but it provides the exact same utility in the end?

All the extra variants being added in this PR is great, but can just as easily be done with thiserror

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two benefits:

  • The context stuff is shorter, especially when adding extra context. This makes it practical to not implement From<UnderlyingError> for MyError and in fact snafu's derive macro never implements this.
  • As a consequence, it becomes more apparent where errors are being converted, so it is much easier to notice / review whether a particular error variant is overused – I've seen some Io and Nix error variants, many of which would likely benefit from being split up.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can omit #[from] when defining variants with thiserror, but adding more friction here is definitely a good thing. The catch all Io, etc patterns are no good. If we can mostly solve that on the library portions of this repo then that'd be a big win, regardless of which error lib we use (I ultimately have no preference, they're effectively the same)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thankful you're looking at this, thank you! I'll jump back on matrix next week and it'd be good to connect on errors. It'd be great to focus on user facing errors next (moss / boulder CLI stuff) with anyhow / eyre. We're using thiserror there too and it's definitely the wrong approach.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an example I have this old draft:

#170

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

Successfully merging this pull request may close these issues.

2 participants