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

Show some inner errors (do not hide them with .context) #1916

Merged
merged 2 commits into from
Sep 26, 2020

Conversation

Hocuri
Copy link
Collaborator

@Hocuri Hocuri commented Sep 16, 2020

No description provided.

@Hocuri Hocuri requested a review from link2xt September 16, 2020 14:11
Copy link
Member

@flub flub left a comment

Choose a reason for hiding this comment

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

Should this not work for all errors that have source? Is a simple way to do that to cast to an anyhow::Error if it not already is and then use it's :# formatter as this already does?

@Hocuri
Copy link
Collaborator Author

Hocuri commented Sep 17, 2020

@flub I am afraid I don't understand what you mean.

@csb0730
Copy link

csb0730 commented Sep 17, 2020

Should this not work for all errors that have source? Is a simple way to do that to cast to an anyhow::Error if it not already is and then use it's :# formatter as this already does?

Did a similar action.
Updated all occurrences where "err" is printed/logged by info! and warn! macros from {} formatter to {:?}
=> This leads all errors to speak, really ;-)

But personally I don't know the detailed difference between {:#} and {:?}

@Hocuri Hocuri force-pushed the show-inner-errors branch 3 times, most recently from ecdbd2c to 249d872 Compare September 19, 2020 11:31
@flub
Copy link
Member

flub commented Sep 19, 2020

I mean we have a bunch of errors which are not anyhow::Error but which do return something when you call Error::source(). We mostly create those by deriving thiserror:

#[derive(Debug, thiserror::Error)]
enum SomeError {
    #[error("msg")]
    SomeVariant(#[from] OrigError),
    #[error("msg")]
    OtherVarian(#[source] OtherOrigError),
}

Here both of these variants will return something when you call Error::source(). It would be nice to also show those errors with their causes/sources as well, even though they are not anyhow::Error errors. I think the simplest way for that is to first wrap them using anyhow::Error::new() and then log it using {:#} as proposed here. But I don't know how you'd know whether you have an std::error::Error here or an anyhow::Error, there probably is a way.

@Hocuri
Copy link
Collaborator Author

Hocuri commented Sep 19, 2020

Now I understand.

As we are mostly using anyhow::Result<_>, most errors will be anyhow::Error. std::error::Error is just a trait, not a type/struct. The only other possibility is some special error that we create with thiserror like deltachat::configure::read_url::Error.

In VSCode + Rustanalyzer you can hover your mouse over a variable and then click Go to Error to find out which error this is. It will probaby be the same for the whole file.

Hmmmm... Do you think we should replace all 101 occurences in 24 files of {}", e with {:#}", e? Or check where anyhow::Error is used first? (should be done in 5min)

@link2xt
Copy link
Collaborator

link2xt commented Sep 19, 2020

I opened an issue in thiserror repository: dtolnay/thiserror#98

I would rather replace only errors which are tested with {:#}, it may not be appropriate in all cases. Causes may be not user-readable, not every library cares about formatting its causes properly.

@flub
Copy link
Member

flub commented Sep 20, 2020

Perhaps for warn!() and info!() it is fine to do a bulk replacement. But error!() it would not be suitable as that's directly shown to users instead of ending up in logs.

It might be possible to write a formatter helper with a signature that accepts Into<anyhow::Error> and does the formatting. Since anyhow::Error impls From<StdError + ...> this could be enough. If that can't work we may need to look at the various downcasting and is() methods of StdError and anyhow::Error

@Hocuri Hocuri merged commit 02baf4b into master Sep 26, 2020
@Hocuri Hocuri deleted the show-inner-errors branch September 26, 2020 20:48
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.

None yet

4 participants