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

main: Handle errors gracefully in main() #122

Closed
wants to merge 1 commit into from

Conversation

jcvenegas
Copy link

@jcvenegas jcvenegas commented May 24, 2021

Handle calls to CLI, VMM::try_from() and vmm.run().

This commit adds anyhow and thiserror crates to
handle errors.

The anyhow crate is used in the main binary to add
extra context and give a pretty view of error trace.

The create thiserror was used to easily
derive std::error::Error for errors provided by libs.

The new crates pulls the following extra crates:

+name = "proc-macro2"
+name = "quote"
+name = "syn"
+name = "thiserror-impl"
+name = "unicode-xid"

New error format:
examples:

$ vmm-reference --kernel \
    path=/path/vmlinuz-5.10.25
Error: Failed to create VMM from configurations

Caused by:
    Error issuing an ioctl to KVM.

$ echo $?
1
$ vmm-reference --kernel path
Error: Failed to parse CLI options

Caused by:
    Failed to parse cli Invalid input for kernel: Missing required
    argument: path
$vmm-reference
Error: Failed to parse CLI options

Caused by:
    Failed to parse cli error: The following required arguments were not provided:
        --kernel <kernel>

    USAGE:
        vmm-reference [OPTIONS] --kernel <kernel>

    For more information try --help

Fixes: #99

Signed-off-by: Carlos Venegas [email protected]

@jcvenegas jcvenegas force-pushed the better-error-handling branch 4 times, most recently from f9b9fad to d0e73b7 Compare May 24, 2021 19:05
Handle calls to `CLI`, `VMM::try_from()` and `vmm.run()`.

This commit adds anyhow and thiserror crates to handle errors.

The `anyhow` crate is used in the main binary to add extra context and
give a pretty view of error trace.

The create `thiserror` was used to easily derive `std::error::Error` for
errors provided by libs.

The new crates pulls the following extra crates: ```diff +name =
"proc-macro2" +name = "quote" +name = "syn" +name = "thiserror-impl"
+name = "unicode-xid" ```

New error format: examples: ```sh $ vmm-reference --kernel \
path=/path/vmlinuz-5.10.25 Error: Failed to create VMM from
configurations

Caused by: Error issuing an ioctl to KVM.

$ echo $?  1 ```

```sh $ vmm-reference --kernel path Error: Failed to parse CLI options

Caused by: Failed to parse cli Invalid input for kernel: Missing
required argument: path ```

``` $vmm-reference Error: Failed to parse CLI options

Caused by: Failed to parse cli error: The following required arguments
were not provided: --kernel <kernel>

    USAGE: vmm-reference [OPTIONS] --kernel <kernel>

    For more information try --help ```

Fixes: rust-vmm#99

Signed-off-by: Carlos Venegas <[email protected]>
@andreeaflorescu
Copy link
Member

We typically do not want to add other dependencies, but I find anyhow and thiserror to reduce quite a few of the burden of dealing with errors, so I am inclined to accept this PR. I also noticed that these are also used by Crosvm & Cloud Hypervisor. As long as we have this dependency only in the VMM glue code, I think it's fine.

@alexandruag @lauralt what do you think?

@jcvenegas jcvenegas closed this Nov 16, 2021
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.

Handle errors gracefully in main()
2 participants