Skip to content

Consider using fs-err to improve I/O error messages #270

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

Closed
edmorley opened this issue Sep 9, 2024 · 3 comments · Fixed by #355
Closed

Consider using fs-err to improve I/O error messages #270

edmorley opened this issue Sep 9, 2024 · 3 comments · Fixed by #355
Labels
enhancement New feature or request

Comments

@edmorley
Copy link
Member

edmorley commented Sep 9, 2024

The fs-err crate aims to improve error messages for I/O errors by eg including the paths of the file/... that triggered the error:
https://crates.io/crates/fs-err

It's supposed to be a drop-in replacement, however, testing locally I found that it made the error message worse in some cases.

For example, before switching to fs-err, the output from the runtime_txt_io_error integration test (whose fixture intentionally has runtime.txt that contains invalid unicode) was:

[Error: Unable to read runtime.txt]
An unexpected error occurred whilst reading the (optional) runtime.txt file.

Details: I/O Error: stream did not contain valid UTF-8

And after:

[Error: Unable to read runtime.txt]
An unexpected error occurred whilst reading the (optional) runtime.txt file.

Details: I/O Error: failed to read from file `/workspace/runtime.txt`

With the error message temporarily changed to print the debug output, we can more clearly see the issue:

[Error: Unable to read runtime.txt]
An unexpected error occurred whilst reading the (optional) runtime.txt file.

Details: I/O Error: Custom { kind: InvalidData, error: Error { kind: Read, source: Error { kind: InvalidData, message: "stream did not contain valid UTF-8" }, path: "/workspace/runtime.txt" } }

ie: fs-err wraps the underlying cause of the io::Error, and that cause is not included in the display representation of the top level error. Instead, one has to manually check for and print error.source(). This is apparently by design (which I get, but it still means more work to use fs-err, and it not quite being the drop-in replacement it promises):
andrewhickman/fs-err#51

It may still be worth using fs-err - however, if we adopt it then further buildpack changes will be required to maintain all error message content (such as having the log_io_error function print a "caused by" line iff error.sources() is Some).

@edmorley
Copy link
Member Author

edmorley commented Sep 9, 2024

Filed andrewhickman/fs-err#59.

@edmorley
Copy link
Member Author

edmorley commented Nov 5, 2024

Filed andrewhickman/fs-err#59.

This was fixed in andrewhickman/fs-err#60 which was released in v3.0.0 of fs-err:
https://github.com/andrewhickman/fs-err/blob/main/CHANGELOG.md#300

edmorley added a commit that referenced this issue Apr 28, 2025
This improves the error messages shown for file related
I/O errors, by:
- ensuring that the path is always included
- adding additional suggestions to the error based on the
  context (which is now known, since specialised types are
  used for each of the common scenarios like checking if
  a file exists or reading a file to a string)

We're able to make the errors richer than those provided
by `fs-err`, since we know the context in which the errors
have occurred. For example, in the error message for
`ReadOptionalFileError`s we know not to say to check for
a missing file or that the file is a directory, since
`read_optional_file()` explicitly excludes those cases by
design. If we used `io::Error` (or `fs-err`'s equivalent)
we wouldn't have to rely on the user not using the wrong
logging function for the wrong I/O error case, rather than
relying on the type system to do that for us.

These changes leave `log_io_error()` as being used only
for the `Command` I/O cases. A later PR will switch adjust
those usages too (but this PR is already large enough).

Closes #270.
GUS-W-12650236.
@edmorley
Copy link
Member Author

Closing in favour of the approach in #355.

@edmorley edmorley closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2025
edmorley added a commit that referenced this issue Apr 29, 2025
This improves the error messages shown for file related I/O errors,
by:
- ensuring that the path is always included
- adding additional suggestions to the error based on the context
  (which is now known, since specialised types are used for each of
  the common scenarios like checking if a file exists or reading a
  file to a string)

We're able to make the errors richer than those provided by `fs-err`,
since we know the context in which the errors have occurred.

For example, in the error message for `ReadOptionalFileError`s we know
not to say to check for a missing file or that the file is a directory,
since `read_optional_file()` explicitly excludes those cases by design.
If we used `io::Error` (or `fs-err`'s equivalent) we would have to rely
on the user not using the wrong logging function for the wrong I/O error
case, rather than relying on the type system to do that for us.

These changes leave `log_io_error()` as being used only for the
`Command` I/O cases. A later PR will switch adjust those usages too
(but this PR is already large enough).

Closes #270.
GUS-W-12650236.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant