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

Guidance for what to write in Error::description() #71

Closed
dtolnay opened this Issue May 24, 2017 · 9 comments

Comments

Projects
None yet
3 participants
@dtolnay
Copy link
Member

dtolnay commented May 24, 2017

My understanding is that nothing should ever use description(). Most errors will have a significantly more helpful Display representation than what the signature of description() allows.

Nevertheless, description() is there and has to return something.

@Enet4

This comment has been minimized.

Copy link

Enet4 commented May 25, 2017

My understanding is that nothing should ever use description()

That's quite a claim. Can you provide some examples of what would be a good practice here? I would actually imagine certain implementations of Display relying on description().

@dtolnay

This comment has been minimized.

Copy link
Member Author

dtolnay commented May 25, 2017

Sure we can make exceptions for the person implementing Error in the following limited circumstances:

  • You can call description to implement Display of the same error type.
  • You can call description to implement description of a different error type.

But the point is that a user of the library should never have an error value and decide to call description on it instead of using Display.

Guidelines for where description vs Display should be called are not in scope for the API guidelines project because they do not affect the API of a library. It is in scope for Clippy so I filed rust-lang/rust-clippy#1780 to follow up over there.

@Kixunil

This comment has been minimized.

Copy link

Kixunil commented May 31, 2017

I personally find description and Display redundant. I (almost?) always end up implementing Display via description()

@dtolnay

This comment has been minimized.

Copy link
Member Author

dtolnay commented May 31, 2017

@Kixunil you're missing out! Display can usually be much more informative than description.

@Kixunil

This comment has been minimized.

Copy link

Kixunil commented May 31, 2017

@dtolnay I actually do it like this: write!(f, "{}: {}", self.description(), self.additional_info) to avoid copy-pasting same thing.

@dtolnay

This comment has been minimized.

Copy link
Member Author

dtolnay commented May 31, 2017

Okay great. That is what #71 (comment) means by "You can call description to implement Display of the same error type."

@Kixunil

This comment has been minimized.

Copy link

Kixunil commented May 31, 2017

Yes, I understand. What I want to say is that description() is probably unneeded. (But we have to support it since it's stable now.)

@dtolnay

This comment has been minimized.

Copy link
Member Author

dtolnay commented May 31, 2017

I agree.

@dtolnay

This comment has been minimized.

Copy link
Member Author

dtolnay commented Oct 30, 2017

Added a note about description() in 0c5f968.

@dtolnay dtolnay closed this Oct 30, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.