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

Overhaul Nix's error types #1446

Merged
merged 4 commits into from Jul 8, 2021
Merged

Overhaul Nix's error types #1446

merged 4 commits into from Jul 8, 2021

Conversation

asomers
Copy link
Member

@asomers asomers commented Jun 6, 2021

For many of Nix's consumers it be convenient to easily convert a Nix
error into a std::io::Error. That's currently not possible because of
the InvalidPath, InvalidUtf8, and UnsupportedOperation types that have
no equivalent in std::io::Error.

However, very few of Nix's public APIs actually return those unusual
errors. So a more useful API would be for Nix's standard error type to
implement Intostd::io::Error, and the few functions that must return
unusual errors like InvalidUtf8 should use bespoke error types.

This commit prototypes that approach by modifying just one function, for
now, to use the new error type.

@asomers asomers marked this pull request as ready for review June 13, 2021 01:10
@asomers asomers changed the title WIP Overhauling Nix's error types Overhaul Nix's error types Jun 13, 2021
For many of Nix's consumers it be convenient to easily convert a Nix
error into a std::io::Error.  That's currently not possible because of
the InvalidPath, InvalidUtf8, and UnsupportedOperation types that have
no equivalent in std::io::Error.

However, very few of Nix's public APIs actually return those unusual
errors.  So a more useful API would be for Nix's standard error type to
implement Into<std::io::Error>.

This commit makes Error a simple NewType around Errno.  For most
functions it's a drop-in replacement.  There are only three exceptions:

* clearenv now returns a bespoke error type.  It was the only Nix
  function whose error couldn't be cleanly mapped onto an Errno.

* sys::signal::signal now returns Error(Errno::ENOTSUP) instead of
  Error::UnsupportedOperation when the user passes an incompatible
  argument to `handler`.

* When a NixPath exceeds PATH_MAX, it will now return
  Error(Errno::ENAMETOOLONG) instead of Error::InvalidPath.

In the latter two cases there is now some abiguity about whether the
error code was generated by Nix or by the OS.  But I think the ambiguity
is worth it for the sake of being able to implement Into<io::Error>.

This commit also introduces Error::Sys() as a migration aid.  Previously
that as an enum variant.  Now it's a function, but it will work in many
of the same contexts as the original.

Fixes nix-rust#1155
Now that Nix's weird error types are eliminated, there's no reason not
to simply use Errno as the Error type.
@asomers
Copy link
Member Author

asomers commented Jul 8, 2021

bors r+

@bors bors bot merged commit 865c748 into nix-rust:master Jul 8, 2021
@asomers asomers deleted the syserror branch July 8, 2021 04:30
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.

None yet

1 participant