Skip to content

Overhaul Nix's error types#1446

Merged
bors[bot] merged 4 commits intonix-rust:masterfrom
asomers:syserror
Jul 8, 2021
Merged

Overhaul Nix's error types#1446
bors[bot] merged 4 commits intonix-rust:masterfrom
asomers:syserror

Conversation

@asomers
Copy link
Copy Markdown
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 requested a review from kamalmarhubi June 13, 2021 01:10
@asomers asomers changed the title WIP Overhauling Nix's error types Overhaul Nix's error types Jun 13, 2021
asomers added 4 commits July 7, 2021 20:33
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
Copy Markdown
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
sync-agent-neo bot pushed a commit to aosp-mirror-neo/platform_packages_modules_Virtualization that referenced this pull request Mar 28, 2026
The nix upgrade changes nix::Error to implement Into<std::io::Error>,
which removes the need for us to do that on our own.
nix-rust/nix#1446 has more details.

Test: Build
Change-Id: Ifed70bbe1fd45bb36bb12077c4a8a6492e6f0de6
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.

1 participant