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

Replace Option::unwrap() with Option::ok_or_else() with descriptive message #56

Merged
merged 4 commits into from
Oct 24, 2023

Conversation

kylecarow
Copy link
Collaborator

@kylecarow kylecarow commented Oct 10, 2023

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 by PathBuf::extension():

The extension is:
- None, if there is no file name;
- None, if there is no embedded .;
- None, if the file name begins with . and has no other .s within;
- Otherwise, the portion of the file name after the final .

edit: I think the updated error message is fine.

@kylecarow kylecarow requested a review from calbaker October 10, 2023 17:04
@kylecarow
Copy link
Collaborator Author

kylecarow commented Oct 10, 2023

@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))?
Copy link
Collaborator

@calbaker calbaker Oct 10, 2023

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.

Copy link
Collaborator Author

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

.extension()
.ok_or_else(|| anyhow!("Unable to parse file extension: {:?}", file))?
.to_str()
.unwrap()
Copy link
Collaborator

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.

Copy link
Collaborator Author

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

@calbaker calbaker merged commit 1bef634 into fastsim-2 Oct 24, 2023
3 checks passed
@calbaker calbaker deleted the to-file-unwrap-error branch October 24, 2023 18:08
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.

2 participants