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

More ErrorKinds for common errnos #79965

Merged
merged 17 commits into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,8 +880,7 @@ impl OpenOptions {
/// This function will return an error under a number of different
/// circumstances. Some of these error conditions are listed here, together
/// with their [`io::ErrorKind`]. The mapping to [`io::ErrorKind`]s is not
/// part of the compatibility contract of the function, especially the
/// [`Other`] kind might change to more specific kinds in the future.
/// part of the compatibility contract of the function.
///
/// * [`NotFound`]: The specified file does not exist and neither `create`
/// or `create_new` is set.
Expand All @@ -895,9 +894,11 @@ impl OpenOptions {
/// exists.
/// * [`InvalidInput`]: Invalid combinations of open options (truncate
/// without write access, no access mode set, etc.).
/// * [`Other`]: One of the directory components of the specified file path
///
/// The following errors don't match any existing [`io::ErrorKind`] at the moment:
/// * One of the directory components of the specified file path
/// was not, in fact, a directory.
/// * [`Other`]: Filesystem-level errors: full disk, write permission
/// * Filesystem-level errors: full disk, write permission
/// requested on a read-only file system, exceeded disk quota, too many
/// open files, too long filename, too many symbolic links in the
/// specified path (Unix-like systems only), etc.
Expand All @@ -913,7 +914,6 @@ impl OpenOptions {
/// [`AlreadyExists`]: io::ErrorKind::AlreadyExists
/// [`InvalidInput`]: io::ErrorKind::InvalidInput
/// [`NotFound`]: io::ErrorKind::NotFound
/// [`Other`]: io::ErrorKind::Other
/// [`PermissionDenied`]: io::ErrorKind::PermissionDenied
#[stable(feature = "rust1", since = "1.0.0")]
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
Expand Down Expand Up @@ -2190,7 +2190,7 @@ impl DirBuilder {
Some(p) => self.create_dir_all(p)?,
None => {
return Err(io::Error::new_const(
io::ErrorKind::Other,
io::ErrorKind::Uncategorized,
&"failed to create whole tree",
));
}
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/fs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,8 @@ fn metadata_access_times() {
match (a.created(), b.created()) {
(Ok(t1), Ok(t2)) => assert!(t1 <= t2),
(Err(e1), Err(e2))
if e1.kind() == ErrorKind::Other && e2.kind() == ErrorKind::Other
if e1.kind() == ErrorKind::Uncategorized
&& e2.kind() == ErrorKind::Uncategorized
|| e1.kind() == ErrorKind::Unsupported
&& e2.kind() == ErrorKind::Unsupported => {}
(a, b) => {
Expand Down
189 changes: 160 additions & 29 deletions library/std/src/io/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ pub enum ErrorKind {
/// The connection was reset by the remote server.
#[stable(feature = "rust1", since = "1.0.0")]
ConnectionReset,
/// The remote host is not reachable.
#[unstable(feature = "io_error_more", issue = "86442")]
HostUnreachable,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// The network containing the remote host is not reachable.
#[unstable(feature = "io_error_more", issue = "86442")]
NetworkUnreachable,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// The connection was aborted (terminated) by the remote server.
#[stable(feature = "rust1", since = "1.0.0")]
ConnectionAborted,
Expand All @@ -119,6 +125,9 @@ pub enum ErrorKind {
/// local.
#[stable(feature = "rust1", since = "1.0.0")]
AddrNotAvailable,
/// The system's networking is down.
#[unstable(feature = "io_error_more", issue = "86442")]
NetworkDown,
/// The operation failed because a pipe was closed.
#[stable(feature = "rust1", since = "1.0.0")]
BrokenPipe,
Expand All @@ -129,6 +138,37 @@ pub enum ErrorKind {
/// requested to not occur.
#[stable(feature = "rust1", since = "1.0.0")]
WouldBlock,
/// A filesystem object is, unexpectedly, not a directory.
///
/// For example, a filesystem path was specified where one of the intermediate directory
/// components was, in fact, a plain file.
#[unstable(feature = "io_error_more", issue = "86442")]
NotADirectory,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// The filesystem object is, unexpectedly, a directory.
///
/// A directory was specified when a non-directory was expected.
#[unstable(feature = "io_error_more", issue = "86442")]
IsADirectory,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// A non-empty directory was specified where an empty directory was expected.
#[unstable(feature = "io_error_more", issue = "86442")]
DirectoryNotEmpty,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// The filesystem or storage medium is read-only, but a write operation was attempted.
#[unstable(feature = "io_error_more", issue = "86442")]
ReadOnlyFilesystem,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// Loop in the filesystem; often, too many levels of symbolic links.
///
/// There was a loop (or excessively long chain) resolving a filesystem object.
///
/// On Unix this is usually the result of a symbolic link loop; or, of exceeding the
/// system-specific limit on the depth of symlink traversal.
#[unstable(feature = "io_error_more", issue = "86442")]
FilesystemLoop,
/// Stale network file handle
///
/// With some network filesystems, notably NFS, an open file (or directory) can be invalidated
/// by problems with the network or server.
#[unstable(feature = "io_error_more", issue = "86442")]
StaleNetworkFileHandle,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// A parameter was incorrect.
#[stable(feature = "rust1", since = "1.0.0")]
InvalidInput,
Expand Down Expand Up @@ -158,19 +198,67 @@ pub enum ErrorKind {
/// [`Ok(0)`]: Ok
#[stable(feature = "rust1", since = "1.0.0")]
WriteZero,
/// The underlying storage (typically, a filesystem) is full.
///
/// This does not include out of quota errors.
#[unstable(feature = "io_error_more", issue = "86442")]
StorageFull,
/// Seek on unseekable file
///
/// Seeking was attempted on an open file handle which is not suitable for seeking - for
/// example, on Unix, a named pipe opened with `File::open`.
#[unstable(feature = "io_error_more", issue = "86442")]
NotSeekable,
/// Filesystem quota was exceeded.
#[unstable(feature = "io_error_more", issue = "86442")]
FilesystemQuotaExceeded,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// File larger than allowed or supported.
///
/// This might arise from a hard limit of the underlying filesystem or file access API, or from
/// an administratively imposed resource limitation. Simple disk full, and out of quota, have
/// their own errors.
#[unstable(feature = "io_error_more", issue = "86442")]
FileTooLarge,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// Resource is busy.
#[unstable(feature = "io_error_more", issue = "86442")]
ResourceBusy,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// Executable file is busy.
///
/// An attempt was made to write to a file which is also in use as a running program. (Not all
/// operating systems detect this situation.)
#[unstable(feature = "io_error_more", issue = "86442")]
ExecutableFileBusy,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// Deadlock (avoided)
///
/// A file locking operation would result in deadlock. This situation is typically detected, if
/// at all, on a best-effort basis.
#[unstable(feature = "io_error_more", issue = "86442")]
Deadlock,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// Cross-device or cross-filesystem (hard) link or rename.
#[unstable(feature = "io_error_more", issue = "86442")]
CrossesDevices,
/// Too many (hard) links to the same filesystem object.
///
/// The filesystem does not support making so many hardlinks to the same file.
#[unstable(feature = "io_error_more", issue = "86442")]
TooManyLinks,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// Filename too long.
///
/// The limit might be from the underlying filesystem or API, or an administratively imposed
/// resource limit.
#[unstable(feature = "io_error_more", issue = "86442")]
FilenameTooLong,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

std uses file_name in functions, rather than filename. It'd be odd to break correspondence between snake_case and CamelCase, so I think this should be FileNameTooLong to match. Same goes for InvalidFileName.

/// Program argument list too long.
///
/// When trying to run an external program, a system or process limit on the size of the
/// arguments would have been exceeded.
#[unstable(feature = "io_error_more", issue = "86442")]
ArgumentListTooLong,
ijackson marked this conversation as resolved.
Show resolved Hide resolved
/// This operation was interrupted.
///
/// Interrupted operations can typically be retried.
#[stable(feature = "rust1", since = "1.0.0")]
Interrupted,
/// Any I/O error not part of this list.
///
/// Errors that are `Other` now may move to a different or a new
/// [`ErrorKind`] variant in the future. It is not recommended to match
/// an error against `Other` and to expect any additional characteristics,
/// e.g., a specific [`Error::raw_os_error`] return value.
#[stable(feature = "rust1", since = "1.0.0")]
Other,

/// An error returned when an operation could not be completed because an
/// "end of file" was reached prematurely.
Expand All @@ -180,6 +268,18 @@ pub enum ErrorKind {
/// read.
#[stable(feature = "read_exact", since = "1.6.0")]
UnexpectedEof,
/// A custom error that does not fall under any other I/O error kind.
///
/// This can be used to construct your own [`Error`]s that do not match any
/// [`ErrorKind`].
///
/// This [`ErrorKind`] is not used by the standard library.
///
/// Errors from the standard library that do not fall under any of the I/O
/// error kinds cannot be `match`ed on, and will only match a wildcard (`_`) pattern.
/// New [`ErrorKind`]s might be added in the future for some of those.
#[stable(feature = "rust1", since = "1.0.0")]
Other,

/// This operation is unsupported on this platform.
///
Expand All @@ -191,31 +291,62 @@ pub enum ErrorKind {
/// to allocate enough memory.
#[stable(feature = "out_of_memory_error", since = "1.54.0")]
OutOfMemory,

/// Any I/O error from the standard library that's not part of this list.
///
/// Errors that are `Uncategorized` now may move to a different or a new
/// [`ErrorKind`] variant in the future. It is not recommended to match
/// an error against `Uncategorized`; use a wildcard match (`_`) instead.
#[unstable(feature = "io_error_uncategorized", issue = "none")]
#[doc(hidden)]
Uncategorized,
}

impl ErrorKind {
pub(crate) fn as_str(&self) -> &'static str {
use ErrorKind::*;
match *self {
ErrorKind::NotFound => "entity not found",
ErrorKind::PermissionDenied => "permission denied",
ErrorKind::ConnectionRefused => "connection refused",
ErrorKind::ConnectionReset => "connection reset",
ErrorKind::ConnectionAborted => "connection aborted",
ErrorKind::NotConnected => "not connected",
ErrorKind::AddrInUse => "address in use",
ErrorKind::AddrNotAvailable => "address not available",
ErrorKind::BrokenPipe => "broken pipe",
ErrorKind::AlreadyExists => "entity already exists",
ErrorKind::WouldBlock => "operation would block",
ErrorKind::InvalidInput => "invalid input parameter",
ErrorKind::InvalidData => "invalid data",
ErrorKind::TimedOut => "timed out",
ErrorKind::WriteZero => "write zero",
ErrorKind::Interrupted => "operation interrupted",
ErrorKind::Other => "other os error",
ErrorKind::UnexpectedEof => "unexpected end of file",
ErrorKind::Unsupported => "unsupported",
ErrorKind::OutOfMemory => "out of memory",
AddrInUse => "address in use",
AddrNotAvailable => "address not available",
AlreadyExists => "entity already exists",
ArgumentListTooLong => "argument list too long",
BrokenPipe => "broken pipe",
ResourceBusy => "resource busy",
ConnectionAborted => "connection aborted",
ConnectionRefused => "connection refused",
ConnectionReset => "connection reset",
CrossesDevices => "cross-device link or rename",
Deadlock => "deadlock",
DirectoryNotEmpty => "directory not empty",
ExecutableFileBusy => "executable file busy",
FilenameTooLong => "filename too long",
FilesystemQuotaExceeded => "filesystem quota exceeded",
FileTooLarge => "file too large",
HostUnreachable => "host unreachable",
Interrupted => "operation interrupted",
InvalidData => "invalid data",
InvalidInput => "invalid input parameter",
IsADirectory => "is a directory",
NetworkDown => "network down",
NetworkUnreachable => "network unreachable",
NotADirectory => "not a directory",
StorageFull => "no storage space",
NotConnected => "not connected",
NotFound => "entity not found",
Other => "other error",
OutOfMemory => "out of memory",
PermissionDenied => "permission denied",
ReadOnlyFilesystem => "read-only filesystem or storage medium",
StaleNetworkFileHandle => "stale network file handle",
FilesystemLoop => "filesystem loop (e.g. symbolic link loop)",
NotSeekable => "seek on unseekable file",
TimedOut => "timed out",
TooManyLinks => "too many links",
Uncategorized => "uncategorized error",
UnexpectedEof => "unexpected end of file",
Unsupported => "unsupported",
WouldBlock => "operation would block",
WriteZero => "write zero",
}
}
}
Expand Down Expand Up @@ -538,7 +669,7 @@ impl Error {
/// }
///
/// fn main() {
/// // Will print "Other".
/// // Will print "Uncategorized".
/// print_error(Error::last_os_error());
/// // Will print "AddrInUse".
/// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,7 @@ pub trait Write {
if output.error.is_err() {
output.error
} else {
Err(Error::new_const(ErrorKind::Other, &"formatter error"))
Err(Error::new_const(ErrorKind::Uncategorized, &"formatter error"))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/net/tcp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ fn double_bind() {
Err(e) => {
assert!(
e.kind() == ErrorKind::ConnectionRefused
|| e.kind() == ErrorKind::Other
|| e.kind() == ErrorKind::Uncategorized
|| e.kind() == ErrorKind::AddrInUse,
"unknown error: {} {:?}",
e,
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/wasi/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,5 +532,6 @@ pub fn symlink_path<P: AsRef<Path>, U: AsRef<Path>>(old_path: P, new_path: U) ->
}

fn osstr2str(f: &OsStr) -> io::Result<&str> {
f.to_str().ok_or_else(|| io::Error::new_const(io::ErrorKind::Other, &"input must be utf-8"))
f.to_str()
.ok_or_else(|| io::Error::new_const(io::ErrorKind::Uncategorized, &"input must be utf-8"))
}
4 changes: 1 addition & 3 deletions library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,8 +1658,7 @@ impl Child {
/// Forces the child process to exit. If the child has already exited, an [`InvalidInput`]
/// error is returned.
///
/// The mapping to [`ErrorKind`]s is not part of the compatibility contract of the function,
/// especially the [`Other`] kind might change to more specific kinds in the future.
/// The mapping to [`ErrorKind`]s is not part of the compatibility contract of the function.
///
/// This is equivalent to sending a SIGKILL on Unix platforms.
///
Expand All @@ -1680,7 +1679,6 @@ impl Child {
///
/// [`ErrorKind`]: io::ErrorKind
/// [`InvalidInput`]: io::ErrorKind::InvalidInput
/// [`Other`]: io::ErrorKind::Other
#[stable(feature = "process", since = "1.0.0")]
pub fn kill(&mut self) -> io::Result<()> {
self.handle.kill()
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/hermit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
x if x == 1 as i32 => ErrorKind::PermissionDenied,
x if x == 32 as i32 => ErrorKind::BrokenPipe,
x if x == 110 as i32 => ErrorKind::TimedOut,
_ => ErrorKind::Other,
_ => ErrorKind::Uncategorized,
}
}

Expand Down
Loading