Improve Interoperability with Error Reporting Tools
Introduction
I recently opened an issue (#354) thinking that there was a bug in this crate, but the problem that I was having tourned out to be caused by a bug from serde. While I was going through the source code of this crate I noticed a minor thing that could, at least according to my personal opinion, be improved. Given the great value that I was able to get from this library, I thought I could attempt to give a small contribution to rust-csv and implemented this PR.
Before describing my proposal, I'd just like to clarify that although I opened a PR, I don't think that the code I'm presenting is necessarily ready to be merged yet. I've submitted it just so that we discuss the changes while looking at something concrete. The code compiles and passes all the tests.
Problem Description
The issue I'm having is with the way in which errors from this crate are formatted in their Display trait. In particular, if any csv::Error has a source error, it will be formatted as "<error description>: <source description>". The resulting string is hard to read, especially when the error chain is long.
Chaining errors descriptions by placing them in the same string and separating them with a colon is a simple, common practice. I was doing the same until a few months ago, when I've come across anyhow and eyre. Now I use them to format my errors and the legibility of my logs has greatly improven as a consequence.
Unfortunately, csv::Error currently doesn't play nicely with these tools. The first reason is, of course, that it manually handles the formatting of its source. The second is that it doesn't implement std::error::Error::source, so that eyre isn't able to follow the error trace and print it.
Proposed Solution
This PR would fix both of the issues I've just described.
-
First of all, it adds an implementation for
std::error::Error::source, which would improve the interoperability with error reporting tools I've just described. It would also allow any client code to usestd::error::Error::sourceand would better model the semantics of the errors thatrust-csvproduces. -
Secondly, it changes the implementation of
Displayforcsv::Errorso that the resulting string doesn't include the source error anymore, leaving this task toeyreand similar libraries. -
I've also updated the tutorial so that now most of the example report their errors with
eyre. I've done it in order to be proactive, but it's the change I'm less confident about: on one hand readers might not want to bothereyre, but on the other if they were to run the examples without it they wouldn't be getting a complete description of the error, which might be confusing. Moreover, one of the tests associated with the tutorial relies on the description of a nested error being present, so it would break ifeyrewere not used.
Finally, I've not added eyre to the cookbook, because it didn't seem worth it, but I can fix this if you prefer.