Skip to content

Replace deprecated 'error-chain' with 'thiserror'#1820

Merged
Enselic merged 4 commits into
sharkdp:masterfrom
Enselic:replace-error-chain-with-anyhow
Aug 26, 2021
Merged

Replace deprecated 'error-chain' with 'thiserror'#1820
Enselic merged 4 commits into
sharkdp:masterfrom
Enselic:replace-error-chain-with-anyhow

Conversation

@Enselic

@Enselic Enselic commented Aug 25, 2021

Copy link
Copy Markdown
Collaborator

We currently depend on error-chain. But error-chain has been deprecated:

So I think getting rid of error-chain will not be controversial. Exactly HOW to get rid of error-chain might be a bit controversial though. To get the discussion started, I here have a proposal on how to do it, which uses thiserror.

I did consider using anyhow, but read somewhere that someone thought that libraries should not use any particular high-level error handling library, but instead let apps pick one, and I thought that made sense. thiserror is just a derive-macro so one does not have to implement Error traits manually.

I have not done broad verification yet, but all regression tests pass, and basic error reporting work as before. Examples:

% for bat in bat bat-pr; do BAT_PAGER=bat $bat examples/simple.rs; done
[bat error]: Use of bat as a pager is disallowed in order to avoid infinite recursion problems
[bat error]: Use of bat as a pager is disallowed in order to avoid infinite recursion problems

% for bat in bat bat-pr; do BAT_PAGER='not parsable"' $bat examples/simple.rs; done 
[bat error]: Could not parse pager command.
[bat error]: Could not parse pager command.

% for bat in bat bat-pr; do $bat --language swedish tests/examples/single-line.txt; done
[bat error]: unknown syntax: 'swedish'
[bat error]: unknown syntax: 'swedish'

% echo --garbage > /home/martin/.config/bat/config
% for bat in bat bat-pr; do $bat tests/examples/single-line.txt; done
error: Found argument '--garbage' which wasn't expected, or isn't valid in this context

USAGE:
    bat [OPTIONS] [FILE]...
    bat <SUBCOMMAND>

For more information try --help
error: Found argument '--garbage' which wasn't expected, or isn't valid in this context

USAGE:
    bat-f85419fab6 [OPTIONS] [FILE]...
    bat-f85419fab6 <SUBCOMMAND>

For more information try --help

@Enselic Enselic force-pushed the replace-error-chain-with-anyhow branch from f85419f to 571e9f7 Compare August 25, 2021 07:22
The reason is simply that String does not implement Error, and that would
not be a good idea. See e.g.:
https://internals.rust-lang.org/t/impl-error-for-string/8881
@sharkdp

sharkdp commented Aug 25, 2021

Copy link
Copy Markdown
Owner

I was never really happy with this .chain_err(|| …) code, so this looks very good to me!

My understanding is that thiserror is for libraries while anyhow is for applications, yes.

We're using thiserror+anyhow in another project of mine (hexyl) which is split into library + application parts.
And we're using anyhow in fd (which is only an application).

I was quite happy with both crates so far.

Comment thread src/error.rs
ParseIntError(::std::num::ParseIntError);
GlobParsingError(::globset::Error);
SerdeYamlError(::serde_yaml::Error);
#[derive(Error, Debug)]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I don't understand what clippys problem is here...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Most likely caused by thiserror allowing some clippy problems that our MSRV does not know about: https://github.com/dtolnay/thiserror/blob/master/impl/src/expand.rs#L120

Best way to fix is probably to --allow clippy::unknown_clippy_lints on MSRV clippy

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

...or make use of clippy::msrv (thanks for the ping)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I ended up using --allow clippy::unknown_clippy_lints since starting to use clippy::msrv is not trivial (requires moving to some other CI job)

@Enselic

Enselic commented Aug 26, 2021

Copy link
Copy Markdown
Collaborator Author

I've now fixed remaining open issue and done some additional verification, and the code seems to work as it should.

So feel free to take another look. If it looks good, we can (squash) merge this, as far as I'm concerned.

@Enselic Enselic requested a review from sharkdp August 26, 2021 07:10

@sharkdp sharkdp left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks!

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