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

tree-wide: improve and refactor error printing #388

Merged
merged 5 commits into from
Jul 11, 2023

Conversation

Ablu
Copy link
Contributor

@Ablu Ablu commented Jul 6, 2023

This PR groups some patches that improve the error printing and reporting mechanisms.

Overall, this reduces the number of .unwrap() calls used and starts to actually
put the pretty error messages that we already have defined into use.

Requirements

Before submitting your PR, please make sure you addressed the following
requirements:

  • All commits in this PR are signed (with git commit -s), and the commit
    message has max 60 characters for the summary and max 75 characters for each
    description line.
  • All added/changed functionality has a corresponding unit/integration
    test.
  • All added/changed public-facing functionality has entries in the "Upcoming
    Release" section of CHANGELOG.md (if no such section exists, please create one).
  • Any newly added unsafe code is properly documented.

@Ablu
Copy link
Contributor Author

Ablu commented Jul 6, 2023

Does this need a CHANGELOG.md entry? It is certainly public-facing, but it does not change anything implementation wise.

@Ablu Ablu force-pushed the error-pretty-print branch from 750301e to 2642f03 Compare July 7, 2023 05:31
vireshk
vireshk previously approved these changes Jul 7, 2023
@Ablu
Copy link
Contributor Author

Ablu commented Jul 7, 2023

Changelog

  • Adjusted coverage. There is little worthwhile to test here (IMHO).

@vireshk vireshk enabled auto-merge (rebase) July 7, 2023 06:09
@Ablu
Copy link
Contributor Author

Ablu commented Jul 7, 2023

I realized that I may have to do a similar update to vsock too. But merging this does not hurt. Will include the vsock fixes in a new PR.

@@ -1,5 +1,5 @@
{
"coverage_score": 69.6,
"coverage_score": 69.0,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is coverage dropping because the tests aren't exercising the error exit path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

It must have been right on the corner of the allowed interval. Tests are likely just starting on the start_foo level since they still want the Results. Of course one could try to write tests for this. But I am not sure how valuable those would be (they would also have to re-test major chunks of what others tests are already testing). I am also not sure how well it would play with the exit(1) call...

@Ablu Ablu mentioned this pull request Jul 7, 2023
4 tasks
crates/rng/src/main.rs Outdated Show resolved Hide resolved
We setup pretty-printing with all the thiserror-based Error structs, but
then only bubble the error up to the main, where it just gets printed
with the `Debug` trait.

This "catches" the error in the main and performs pretty printing using
the `Display` trait.

Before:

    Error: FailedCreatingListener(SocketError(Os { code: 98, kind:
        AddrInUse, message: "Address already in use" }))

After:

    [2023-07-06T17:20:47Z ERROR vhost_device_scsi] Failed creating
        listener: socket error: Address already in use (os error 98)

vhost-device-vsock is a bit special since it does not let error messages
bubble up to the main. It also does .unwrap() in most places, but it
_does_  pretty print errors during the main request handling part.

Had to slightly adjust the coverage since we have no tests for the main
functions.

Signed-off-by: Erik Schilling <[email protected]>
auto-merge was automatically disabled July 10, 2023 06:52

Head branch was pushed to by a user without write access

@Ablu Ablu force-pushed the error-pretty-print branch from 2642f03 to 4eccd95 Compare July 10, 2023 06:52
Copy link
Contributor Author

@Ablu Ablu left a comment

Choose a reason for hiding this comment

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

Changelog

It looks like the error handling is not properly tested, which now shows in degraded code coverage. I will see whether I can add some tests to cover that.

crates/rng/src/main.rs Outdated Show resolved Hide resolved
@Ablu
Copy link
Contributor Author

Ablu commented Jul 10, 2023

It looks like the error handling is not properly tested, which now shows in degraded code coverage. I will see whether I can add some tests to cover that.

After looking into this: It is not as easy... Testing these corner-cases would need to deal with actual gpio / i2c devices. So we would need to enable the kernel mocking mechanisms for this. So I think I would just consider the ~1% coverage drop as coverage that was already missing previously (for unwrap() coverage did not care about whether the error case was hit or not).

@Ablu Ablu changed the title tree-wide: actually pretty-print error messages tree-wide: improve and refactor error printing Jul 10, 2023
crates/gpio/src/backend.rs Outdated Show resolved Hide resolved
Ablu added a commit to Ablu/vhost-device that referenced this pull request Jul 10, 2023
I plan to add some wrapper errors around vhost errors. These end up
nesting other errors all the way to std::error::Error, which has no
Eq trait.

The implementations were only used for comparisions in tests. While
there is a assert_matches!() in nightly [1] it seems unlikely that
further testing lib additions are getting standarized soon (or ever).

One could use assert!(matches!()), but that would worsen the error
messages for test failures. Hence, during review [2] we agreed on
introducing the assert_matches crate. It got no dependencies and
allows us to keep the good error messages while not needing to depend
on nightly.

[1] https://doc.rust-lang.org/std/assert_matches/macro.assert_matches.html
[2] rust-vmm#388 (comment)

Signed-off-by: Erik Schilling <[email protected]>
@Ablu Ablu force-pushed the error-pretty-print branch from 4eccd95 to bb0eb21 Compare July 10, 2023 08:59
@Ablu
Copy link
Contributor Author

Ablu commented Jul 10, 2023

Changelog

  • Introduced use of assert_matches crate (as discussed above)

Ablu added 3 commits July 10, 2023 11:23
I plan to add some wrapper errors around vhost errors. These end up
nesting other errors all the way to std::error::Error, which has no
Eq trait.

The implementations were only used for comparisions in tests. While
there is a assert_matches!() in nightly [1] it seems unlikely that
further testing lib additions are getting standarized soon (or ever).

One could use assert!(matches!()), but that would worsen the error
messages for test failures. Hence, during review [2] we agreed on
introducing the assert_matches crate. It got no dependencies and
allows us to keep the good error messages while not needing to depend
on nightly.

Signed-off-by: Erik Schilling <[email protected]>

[1] https://doc.rust-lang.org/std/assert_matches/macro.assert_matches.html
[2] rust-vmm#388 (comment)
There was a mix of just unwrapping (panicking) and catching and logging
errors. The unwrapping is not allowing for particulary pretty error
handling, so let's bubble the errors up by not crashing the thread,
but by just returning a Result<()> than is received when joining the
threads.

Not all .unwrap() uses were translated since a followup PR (rust-vmm#389) will
rework that code anyway (and get rid of the .unwrap() in the process).

Signed-off-by: Erik Schilling <[email protected]>
If the parsed commandline options cannot be converted into a
`VuRngConfig` we would only get a .unwrap() error message.

Chain this before the check of the daemon launch.

Suggested-by: Manos Pitsidianakis <[email protected]>
Signed-off-by: Erik Schilling <[email protected]>
@Ablu Ablu force-pushed the error-pretty-print branch from bb0eb21 to 15fa85c Compare July 10, 2023 09:23
@Ablu
Copy link
Contributor Author

Ablu commented Jul 10, 2023

Fixed the commit log linter by moving things after the Sign-Off again... I really want rust-vmm/rust-vmm-ci#130 ...

@vireshk
Copy link
Collaborator

vireshk commented Jul 11, 2023

@stefano-garzarella can you approve this too ?

@vireshk vireshk enabled auto-merge (rebase) July 11, 2023 06:58
@vireshk vireshk merged commit 3cab62e into rust-vmm:main Jul 11, 2023
vireshk pushed a commit that referenced this pull request Jul 11, 2023
I plan to add some wrapper errors around vhost errors. These end up
nesting other errors all the way to std::error::Error, which has no
Eq trait.

The implementations were only used for comparisions in tests. While
there is a assert_matches!() in nightly [1] it seems unlikely that
further testing lib additions are getting standarized soon (or ever).

One could use assert!(matches!()), but that would worsen the error
messages for test failures. Hence, during review [2] we agreed on
introducing the assert_matches crate. It got no dependencies and
allows us to keep the good error messages while not needing to depend
on nightly.

Signed-off-by: Erik Schilling <[email protected]>

[1] https://doc.rust-lang.org/std/assert_matches/macro.assert_matches.html
[2] #388 (comment)
@Ablu Ablu mentioned this pull request Oct 6, 2023
4 tasks
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.

5 participants