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

fix(swarm): display cause of denied listen error #4232

Merged
merged 11 commits into from
Jul 24, 2023

Conversation

divagant-martian
Copy link
Contributor

@divagant-martian divagant-martian commented Jul 24, 2023

Description

The display of ListenError could be more helpful. The inner cause already implements Error which in turn requires Display. This is then just a matter of using said impl requirement to get an useful display

Notes & open questions

n/a

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

@divagant-martian divagant-martian changed the title swarm: display cause of denied listen error fix(swarm): display cause of denied listen error Jul 24, 2023
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks @divagant-martian. Though needs a swarm/Cargo.toml version bump. Otherwise it will not be included in the next round of releases.

swarm/CHANGELOG.md Outdated Show resolved Hide resolved
mxinden
mxinden previously approved these changes Jul 24, 2023
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

Thank you.

@divagant-martian
Copy link
Contributor Author

@mxinden do I need to continuously update with master or is this not an issue (as long as there are no conflicts) ?

@mxinden
Copy link
Member

mxinden commented Jul 24, 2023

No need to keep it updated as long as there are no merge conflicts.

The pull request is part of the mergify merge queue now (see last check). Once it is first in the merge queue, mergify will merge latest master into your branch and on green merge.

@mergify mergify bot dismissed mxinden’s stale review July 24, 2023 16:57

Approvals have been dismissed because the PR was updated after the send-it label was applied.

@divagant-martian
Copy link
Contributor Author

latest commit attends to this ci failure

@mergify mergify bot merged commit da743ec into libp2p:master Jul 24, 2023
64 of 66 checks passed
@@ -1684,8 +1684,8 @@ impl fmt::Display for ListenError {
ListenError::Transport(_) => {
write!(f, "Listen error: Failed to negotiate transport protocol(s)")
}
ListenError::Denied { .. } => {
write!(f, "Listen error")
ListenError::Denied { cause } => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This cause is already available as part of calling Error::source. Including it in the fmt::Display as well is kind of an anti-pattern because it leads to double-printing of errors.

If we print it here, we should remove it from Error::source. Also, if you use something like anyhow, the source would be printed which I personally find the better approach than printing it here because stringifies as "late" as possible.

Copy link
Contributor Author

@divagant-martian divagant-martian Jul 29, 2023

Choose a reason for hiding this comment

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

first let me explain the reason for this: we get an error, we print it and get no information at all about this. The implementation of Display comes before the one for Error (since Error: Display ) so it should be up to the Error implementation to deal with this differently if necessary.

If trait A depends on trait B where B on itself is useful and publicly available, I would argue it's an anti-pattern to decide B's implementation just to satisfy something about A. More so of if that comes down to removing important information

Copy link
Contributor

Choose a reason for hiding this comment

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

it should be up to the Error implementation to deal with this differently if necessary.

The issue with that is that it is quite hard to go from a string implementation back to structured data.

This is btw also the "official" recommendation, see rust-lang/project-error-handling#27 (comment).

What is missing the Rust ecosystem is a default way of printing errors. Yes, errors can be displayed but if I understand the error handling project group correctly, then at some point, there will be a std-built-in way of printing an error that will correctly chain them together. Until then, users are encouraged to use things like anyhow or eyre.

Personally, I mildly prefer to keep the display impls short and instead return sources from Error::source. This is still correct from an API point of view: This particular error itself says what happened, i.e. it explains one layer of the entire stack. But looking at errors out of context is seldomly meaningful which is why one should go down the chain of sources.

If trait A depends on trait B where B on itself is useful and publicly available, I would argue it's an anti-pattern to decide B's implementation just to satisfy something about A. More so of if that comes down to removing important information

I'd say it is exactly the opposite actually. By stringifying the error + its sources in the Display impl, the Display impl (which is more general than the Error trait), makes an assumption what it is going to be used for (printing chains of errors) when technically, it shouldn't know about the concept of a "source error".


I am not too fussed which way we go until there is a definite answer from the Rust project but what I want to avoid is double printing of errors when used with a library like anyhow. To avoid this, the inner error needs to be removed from source. Are you open to sending a patch for this?

thomaseizinger pushed a commit that referenced this pull request Aug 20, 2023
The display of `ListenError` could be more helpful. The inner `cause` already implements `Error` which in turn requires `Display`. This is then just a matter of using said impl requirement to get an useful display

Pull-Request: #4232.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants