-
-
Notifications
You must be signed in to change notification settings - Fork 298
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
Add custom error handling #104
Conversation
So, what we're trying to do here is forward all errors to the user and provide useful hints about what to do about the ones we know. Which is currently only the permission error. We also throw a custom error for an unknown interface (because pnet doesn't do it for us). So essentially, what we'd like to get to is a generic error type that would wrap or mirror the io error, and allow us to slap on our own "unknown interface error". Makes sense? (sorry if the explanation is not 100%, I'm a little tired and didn't want to keep you stuck until I have more time :) ). |
src/os/shared.rs
Outdated
)), | ||
Err(e) => Err(e), | ||
Ok(_) => { | ||
let error = MyError::new(MyErrorKind::PermissionError("Please do something".to_string())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imsnif What do you think about this approach? Here we can define our own kind of error along with error message.
GItHub warns about files conflict, but I have no clue how to fix it. There is no conflicts on my local GitBash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @SerhiiBilyk, the conflicts appear because GitHub merges your work with the current master. To reproduce this locally, you need the latest changes.
You will have to add this repo (as opposed to your fork) as a remote if you didn't do it already. See here for instructions. After that, you can either rebase your branch on top of the current master, or merge master with your branch.
I hope this helps!
@ebroto Thank you very much! |
Hey @SerhiiBilyk - is this ready for a review? |
Hey @imsnif , yes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, good progress! I like the approach - just be sure to place the errors in the right place. :) You can check with the permission error (the most important to us right now) locally as I wrote in the comments.
Otherwise, could we change the name from MyError
to something like GetInterfaceError
?
src/os/shared.rs
Outdated
let error = MyError::new(MyErrorKind::PermissionError("Please do something".to_string())); | ||
Err(error) | ||
}, | ||
Err(e) => Err(MyError::new(MyErrorKind::OtherError(e.to_string()))), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The permission error actually happens here. You can test for it locally by running without sudo
and seeing where the error comes from.
@imsnif I've changed error name. It is looks like it work. Could you please check? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, thanks for changing the name. Good progress - I left some comments about what needs to be changed. Please let me know if it's unclear and I'll try to explain further.
src/os/shared.rs
Outdated
)), | ||
Err(e) => Err(e), | ||
Ok(_) => { | ||
let error = GetInterfaceError::new(GetInterfaceErrorKind::OtherError("Please do something".to_string())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here we can go with "Unsupported interface type" as it was before
src/os/shared.rs
Outdated
let error = GetInterfaceError::new(GetInterfaceErrorKind::OtherError("Please do something".to_string())); | ||
Err(error) | ||
}, | ||
Err(e) => Err(GetInterfaceError::new(GetInterfaceErrorKind::PermissionError("Permission error".to_string()))), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Permission error is only one error type we can get here. Essentially, we would like to forward all of these errors to the user, and if the error is a permission error, display the "use sudo" string below. Does this make sense?
@imsnif could you please check if everything is ok ? |
Hey @SerhiiBilyk - correct me if I'm wrong, but I think even after the changes we still send a permission error for whatever error we get, don't we? |
Yes, exactly.
Ah, sorry. I think I wrote that quickly and I wasn't very clear. We want to handle only the permission error (by suggesting to the user to user |
src/os/shared.rs
Outdated
ErrorKind::PermissionDenied => Err(GetInterfaceError::new( | ||
GetInterfaceErrorKind::PermissionError(format!("{}. Try running with sudo.", e)), | ||
)), | ||
_ => Err(GetInterfaceError::new( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imsnif I did not find Result error type which will allow to return here simple std::io::Error Err(e)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we might be able to store its text in the GetInterfaceError you created, so that we can display it to the user?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imsnif If user will run cargo build
without sudo, he will see Permission error (sudo)
.
If you comment 46-48 lines and run cargo build
without sudo, you will see Failed to find any network
, because in this case we throw our custom error only in case of Permission Error.
If you want print to user other error Err(e)
, we should change this failure::bail!("Failed to find any network interface to listen on.");
Answer on your question: yes.
Check this 35 line of errors.rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you think we should change it, let's do that. :)
The final end goal is:
- If it's a permission error, the user should get a permission error and have
sudo
suggested to them. - If there's another error, let's forward it to the user.
- If we get several other errors, we can forward all of them to the user. Maybe one per line with the name of the network interface in front of it.
How does that sound?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. I will do that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, this is great! I'm good to go with this, just one little nitpick:
With this, if there is more than 1 interface that has an error, we will bail and show the error for just the first one.
Would you like to fix this, or shall we leave it for next time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imsnif I will do it in scope of this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Just another addition: if there is at least one permission error, let's just show the permission message. I think that would be clearer. Sounds good?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imsnif Could you please check ?
Hey @captain-yossarian. First let me start by apologizing for my spotty availability over the past few weeks. I really liked the changes you made for displaying multiple errors. I also learned about the I made some minor changes, mainly in order to display the network interface name on all errors, and to make sure all the errors we get are displayed in some edge cases (eg. permission errors on multiple interfaces). In order to find these edge cases, I mostly just started hacking away at the I found, for example, that if we get a permission error, we get a nice explanation for it, but then afterwards we just get the printed errors from the interfaces (sometimes without the interface names). I thought that output could be friendlier, so I added an "Additional Errors" display. Thank you for all your work on this, and your persistence even when I wasn't 100% available to assist. I hope to see more contributions from you in the future. |
@imsnif Hi, thank you for your feedback. I have learn a lot from your codebase! This is my first Rust PR. So thank you very much for your patient and guidance. |
Regarding #73 issue
It is only prototype.
@imsnif Which cases I have to consider in custom error trait?