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

Return errors instead of panicking #130

Merged
merged 3 commits into from
Jul 20, 2023

Conversation

PetrichorIT
Copy link
Contributor

See Issue #127.

@PetrichorIT
Copy link
Contributor Author

On a related note:
It may be beneficial to apply the error-over-panic mentality to more cases, notably TcpListener::bind and UdpSocket::bind. Should we do this within this PR ?

@mcches
Copy link
Contributor

mcches commented Jul 18, 2023

On a related note: It may be beneficial to apply the error-over-panic mentality to more cases, notably TcpListener::bind and UdpSocket::bind. Should we do this within this PR ?

Yes, that makes sense to me.

Copy link
Contributor

@mcches mcches left a comment

Choose a reason for hiding this comment

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

This looks good, but I'm still thinking ip version mismatch is a panic with the current setup.

src/net/tcp/listener.rs Show resolved Hide resolved
src/net/tcp/listener.rs Show resolved Hide resolved
src/top.rs Outdated
link.enqueue_message(&self.config, rand, src, dst, message);
Ok(())
} else {
Err(Error::new(ErrorKind::ConnectionRefused, "host unreachable"))
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: The error message renders like this:

Err(Os { code: 111, kind: ConnectionRefused, message: "Connection refused" })

tests/udp.rs Outdated
let mut sim = Builder::new().ip_version(IpVersion::V6).build();
sim.client("client", async move {
let _sock = bind_to_v4(0).await?;
let sock = bind_to_v4(0).await;
assert!(sock.is_err());
Copy link
Contributor

Choose a reason for hiding this comment

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

Use unwrap_err here as well to simplify?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, since UdpSocket does not impl Debug. If could however combine it with the later let else

@mcches mcches merged commit 6fd75d7 into tokio-rs:main Jul 20, 2023
@mcches mcches linked an issue Jul 20, 2023 that may be closed by this pull request
@PetrichorIT PetrichorIT deleted the error-instead-of-panic branch July 27, 2023 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return errors instead of panicking, when sending invalid packets.
2 participants