dtolnay / thiserror

derive(Error) for struct and enum error types

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Print all source errors for alternate format `{:#}`

kellerb opened this issue · comments

Would it be possible to have the alternate format, {:#}, print all the source errors, like anyhow::Error does?

When logging errors, we want to include the full stack of errors, which means when logging anyhow errors, we use either alternate or debug format, and when using thiserror errors, we use display because we've manually included the source error in the display string. This means the author of each error log statement needs to be aware of this and remember what type of error is being logged. It also leads to the source error of thiserror being logged twice if it is e.g. wrapped in an anyhow::Context.

Thank you, these libraries are very nice.

For your use case would it be possible to convert all errors uniformly to anyhow::Error right before logging them? If you have some error! macro through which the logging happens, have it do anyhow!($err) on the error expression. If it's a function, have it take an Into<anyhow::Error> trait bound.

This sort of rendering is supposed to be done by a central error reporting type, not by individual Display impls of all your errors, or else you tend to get duplication of messages (as you observed).

Ooh, thanks for the error/reporting error distinction. It sounds like we will be able to do the anyhow conversion.