-
Notifications
You must be signed in to change notification settings - Fork 6
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
Replace Option::unwrap() with Option::ok_or_else() with descriptive message #56
Conversation
@calbaker its also possible for OsStr.to_str() to panic at the unwrap, because OsStr can hold non-utf8 data and str cannot But I don't think it would be worth making a specific error message just for the case someone provides non-utf8 data in the file extension or something |
match file.extension().unwrap().to_str().unwrap() { | ||
match file | ||
.extension() | ||
.ok_or_else(|| anyhow!("Unable to parse file extension: {:?}", file))? |
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.
@kylecarow , I think you could change
ok_or_else(|| anyhow!("Unable to parse file extension: {:?}", file))?
to something like
ok_or(|e| anyhow!("{e}\nUnable to parse file extension: {:?}", e, file))?
to preserve whatever the native error message is as context.
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.
Hm, I don't know that the native error message really exists. The unwrap
was on an Option
, not a Result
, so the error was just about unwrapping on a None
rust/fastsim-core/src/traits.rs
Outdated
.extension() | ||
.ok_or_else(|| anyhow!("Unable to parse file extension: {:?}", file))? | ||
.to_str() | ||
.unwrap() |
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.
@kylecarow , I'd lean toward also getting rid of this unwrap with a separate message since it's not much additional effort.
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.
@calbaker done, see latest commit
The only thing I would want your input on this the verbiage of the new error message:File extension not specified
versus the possible times a
None
is returned byPathBuf::extension()
:edit: I think the updated error message is fine.