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

ref(core): Refactor core:fs error mapping to use unified format #17719

Merged
merged 2 commits into from
Feb 16, 2023
Merged

ref(core): Refactor core:fs error mapping to use unified format #17719

merged 2 commits into from
Feb 16, 2023

Conversation

kamilogorek
Copy link
Contributor

Follow-up to #17710

I double-checked the format and name, and there should be no errors during refactoring. Trimmed down the error mapping code by 1/3rd.

@aapoalas
Copy link
Collaborator

Related: #17526 (comment)

})?;
let std_file = open_options
.open(&path)
.map_err(create_err_mapper(format!("open '{}'", path.display())))?;
Copy link
Member

Choose a reason for hiding this comment

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

This will always allocate a string for error even if error is not returned. Could you instead change these to use closure instead so they are lazily evaluated?

Copy link
Contributor Author

@kamilogorek kamilogorek Feb 12, 2023

Choose a reason for hiding this comment

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

On the one hand, this looks ugly as hell:

.map_err(|err| create_err_mapper(format!("open '{}'", path.display()))(err))?;

But this one isn't any better as well :(

fn create_err_mapper(desc_fn: impl Fn() -> String) -> impl Fn(Error) -> Error {
  move |err: Error| Error::new(err.kind(), format!("{err}, {}", desc_fn()))
}

// ...

.map_err(create_err_mapper(|| format!("open '{}'", path.display())))?;

Any preferences?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The first one would be fairly pretty if it were not for the create_err_mapper usage which here seems a bit redundant as it's being created inline in a lambda anyway.

Would it be possible to write a secondary err_mapper function for use in these "inline" cases when there is no real benefit in creating an error mapper once and using it in many places?

.map_err(|err| default_err_mapper(format!("open '{}'", path.display()), err))

@kamilogorek
Copy link
Contributor Author

Done, this should do the work.

@aapoalas aapoalas enabled auto-merge (squash) February 16, 2023 19:20
@aapoalas
Copy link
Collaborator

Nice work, thank you very much!

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.

3 participants