Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upImprove error messages for io::fs #14629
Conversation
alexcrichton
reviewed
Jun 3, 2014
| @@ -134,6 +135,8 @@ impl File { | |||
| last_nread: -1 | |||
| } | |||
| }) | |||
| }).update_err("Couldn't open file", |e| { | |||
This comment has been minimized.
This comment has been minimized.
alexcrichton
Jun 3, 2014
Member
Our convention, at least for the compiler, is to start with lowercase letters as opposed to uppercase letters.
alexcrichton
reviewed
Jun 3, 2014
| @@ -134,6 +135,8 @@ impl File { | |||
| last_nread: -1 | |||
| } | |||
| }) | |||
| }).update_err("Couldn't open file", |e| { | |||
| format!("{}; path={}; mode={}; access={}", e, path.display(), mode_string(mode), access_string(access)) | |||
This comment has been minimized.
This comment has been minimized.
alexcrichton
reviewed
Jun 3, 2014
| } | ||
|
|
||
| //fn io_err_for_path(path: &Path) -> (|&mut IoError| -> String) { | ||
| //|err| format!("{}; path={}", err, path.display()) | ||
| //} |
This comment has been minimized.
This comment has been minimized.
alexcrichton
reviewed
Jun 3, 2014
| super::ReadWrite => "readwrite" | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[allow(unused_imports)] | ||
| mod test { |
This comment has been minimized.
This comment has been minimized.
alexcrichton
Jun 3, 2014
Member
Could you add some tests to make sure there's some more contextual information in the errors returned?
alexcrichton
reviewed
Jun 3, 2014
| fn update_desc(self, desc: &'static str) -> IoResult<T> { | ||
| self.map_err(|mut e| { e.desc = desc; e }) | ||
| } | ||
| } |
This comment has been minimized.
This comment has been minimized.
alexcrichton
Jun 3, 2014
Member
This is an interesting pattern, I wonder if it could be generalized? Not a worry for now, though, it's just a private detail.
alexcrichton
reviewed
Jun 6, 2014
| Some(ref s) => write!(fmt, " ({})", *s), | ||
| None => Ok(()) | ||
| match *self { | ||
| IoError { kind: OtherIoError, desc: "unknown error", detail: Some(ref detail), .. } => |
This comment has been minimized.
This comment has been minimized.
alexcrichton
Jun 6, 2014
Member
I'm actually a little surprised that the compiler lets you get away with , .. here, you matched all the fields!
This comment has been minimized.
This comment has been minimized.
alexcrichton
reviewed
Jun 6, 2014
| detail: if detail { | ||
| Some(os::error_string(errno)) | ||
| detail: if detail && kind == OtherIoError { | ||
| Some(os::error_string(errno).as_slice().chars().map(|c| c.to_lowercase()).collect()) |
This comment has been minimized.
This comment has been minimized.
alexcrichton
Jun 6, 2014
Member
Did you lowercase just to stick with our same conventions? I'd be wary about changing what the OS is giving us, and if it's against our conventions it's not really our fault.
This comment has been minimized.
This comment has been minimized.
wycats
Jun 6, 2014
Author
Contributor
Yeah. I think we should try to have consistency between error messages that are produced in userland (like in libuv, for example) and ones that are produced by the OS.
alexcrichton
reviewed
Jun 6, 2014
| @@ -303,6 +303,8 @@ impl IoError { | |||
| /// struct is filled with an allocated string describing the error | |||
| /// in more detail, retrieved from the operating system. | |||
| pub fn from_errno(errno: uint, detail: bool) -> IoError { | |||
| use str::Str; | |||
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
alexcrichton
reviewed
Jun 6, 2014
| macro_rules! error( ($e:expr, $s:expr) => ( | ||
| match $e { | ||
| Ok(val) => fail!("Should have been an error, was {:?}", val), | ||
| Err(ref err) => assert!(err.to_str().as_slice().contains($s.as_slice()), format!("`{}` did not contain `{}`", err, $s)) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
alexcrichton
reviewed
Jun 6, 2014
| |e| format!("{}; path={}", e, file.path.display())) | ||
| } | ||
|
|
||
| let result: IoResult<int> = update_err(self.fd.read(buf), self); |
This comment has been minimized.
This comment has been minimized.
alexcrichton
Jun 6, 2014
Member
Is this type ascription necessary? I would have thought that rustc could figure it out.
alexcrichton
reviewed
Jun 6, 2014
| @@ -452,6 +481,8 @@ pub fn mkdir(path: &Path, mode: FilePermission) -> IoResult<()> { | |||
| /// directory at the provided path, or if the directory isn't empty. | |||
| pub fn rmdir(path: &Path) -> IoResult<()> { | |||
| LocalIo::maybe_raise(|io| io.fs_rmdir(&path.to_c_str())) | |||
| .update_err("couldn't remove directory", | |||
| |e| format!("{}; path={}", e, path.display())) | |||
This comment has been minimized.
This comment has been minimized.
alexcrichton
Jun 6, 2014
Member
Stylistically, we normally line up the | right after the open-paren so the arguments are all aligned, like:
.update_err("...",
|e| ...);
This comment has been minimized.
This comment has been minimized.
wycats
Jun 6, 2014
Author
Contributor
I feel like there is a conspiracy to make each method call at least 10 lines ;)
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I believe I have responded to all of your comments. |
This comment has been minimized.
This comment has been minimized.
alexcrichton
commented on 5ae6e93
Jun 6, 2014
|
r+ |
This comment has been minimized.
This comment has been minimized.
|
saw approval from alexcrichton |
This comment has been minimized.
This comment has been minimized.
|
merging wycats/rust/improve-fs-errors = 5ae6e93 into auto |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Jun 7, 2014
This comment has been minimized.
This comment has been minimized.
|
I'll rebase and take over from here (talked with @wycats on IRC) |
wycats commentedJun 3, 2014
This commit improves the error descriptions and details for the functions and methods exposed in
io::fs.The basic idea is that the description should always map onto the high-level operation that the user was trying to perform, with the
detailcontaining the original message. TheIoErrorKindis always maintained. Thedetailalso includes additional information (most notably the relevant path, but also other parameters or contextual information).I added an
UpdateIoErrortrait to make it easier to do these in-place modification, and I'm eager for feedback on the specific strategy.