Hi,
I’d like to propose conventions for and enhancements of std::error::Error
trait.
-
.description()
and.detail()
should contain only single-line messages, i. e. without\n
characters (so program output won’t be garbled) -
.detail()
is expected to be appended to.description()
, not used instead of it -
Error
trait should extendShow
- Default implementation of
Show
should be this:
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self.detail() {
Some(detail) => try!(write!(fmt, "{} ({})", self.description(), detail)),
None => try!(write!(fmt, "{}", self.description())),
};
match self.cause() {
Some(cause) => write!(fmt, " caused by: {}", cause),
None => Ok(()),
}
}
I propose conventions to be added to rustdoc of Error
trait. AFAIU default impementation of Show
for Error
is not possible in current rust, so default fmt
can be implemented like this:
impl Error {
fn default_fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { ... }
}
so any type extending Error
trait can reuse default_fmt
to implement Show
.
Note, that because default_fmt
outputs the whose cause chain, so it may output really long message in single line. I think that’s OK, because usually it is important to provide as much error details as possible.
What do you think?