generated from rust-vmm/crate-template
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
main: Handle errors gracefully in main()
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: #99 Signed-off-by: Carlos Venegas <[email protected]>
- Loading branch information
Showing
7 changed files
with
134 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ edition = "2018" | |
|
||
[dependencies] | ||
clap = "2.33.3" | ||
thiserror = "1.0" | ||
|
||
vmm = { path = "../vmm" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,44 @@ | ||
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause | ||
#[cfg(target_arch = "x86_64")] | ||
use std::convert::TryFrom; | ||
#[cfg(target_arch = "x86_64")] | ||
use std::env; | ||
|
||
#[cfg(target_arch = "x86_64")] | ||
use api::CLI; | ||
#[cfg(target_arch = "x86_64")] | ||
use vmm::VMM; | ||
mod main { | ||
use std::convert::TryFrom; | ||
use std::env; | ||
|
||
use api::CLI; | ||
use vmm::VMM; | ||
|
||
fn main() { | ||
#[cfg(target_arch = "x86_64")] | ||
{ | ||
match CLI::launch( | ||
use anyhow::Context; | ||
|
||
pub type Result<T> = anyhow::Result<T>; | ||
|
||
pub fn run() -> Result<()> { | ||
let vmm_config = CLI::launch( | ||
env::args() | ||
.collect::<Vec<String>>() | ||
.iter() | ||
.map(|s| s.as_str()) | ||
.collect(), | ||
) { | ||
Ok(vmm_config) => { | ||
let mut vmm = | ||
VMM::try_from(vmm_config).expect("Failed to create VMM from configurations"); | ||
// For now we are just unwrapping here, in the future we might use a nicer way of | ||
// handling errors such as pretty printing them. | ||
vmm.run().unwrap(); | ||
} | ||
Err(e) => { | ||
eprintln!("Failed to parse command line options. {}", e); | ||
} | ||
} | ||
) | ||
.context("Failed to parse CLI options")?; | ||
|
||
let mut vmm = | ||
VMM::try_from(vmm_config).context("Failed to create VMM from configurations")?; | ||
|
||
vmm.run().context("failed to run VMM") | ||
} | ||
} | ||
|
||
#[cfg(target_arch = "aarch64")] | ||
mod main { | ||
pub type Result<T> = std::result::Result<T, String>; | ||
pub fn run() -> Result<()> { | ||
println!("Reference VMM under construction!"); | ||
Ok(()) | ||
} | ||
#[cfg(target_arch = "aarch64")] | ||
println!("Reference VMM under construction!") | ||
} | ||
|
||
fn main() -> main::Result<()> { | ||
main::run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ authors = ["rust-vmm AWS maintainers <[email protected]>"] | |
edition = "2018" | ||
|
||
[dependencies] | ||
thiserror = "1.0" | ||
|
||
event-manager = "0.2.1" | ||
kvm-bindings = { version = "0.4.0", features = ["fam-wrappers"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters