summary refs log tree commit diff
path: root/vhost
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@chromium.org>2019-02-12 17:51:26 -0800
committerchrome-bot <chrome-bot@chromium.org>2019-02-20 08:20:02 -0800
commitb4bd00fdad094b16c785b8ead9f92d68264f5fb4 (patch)
tree540ad033c789aa08a1ffed17e9c161df331600d2 /vhost
parent0373b9f154b5ec1894138b7c10ad495fcce7b64f (diff)
downloadcrosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar
crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.gz
crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.bz2
crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.lz
crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.xz
crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.zst
crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.zip
error: Print errors using Display impl
I have been running into Debug-printed error messages too often and
needing to look up in the source code each level of nested errors to
find out from the comment on the error variant what the short name of
the variant means in human terms. Worse, many errors (like the one shown
below) already had error strings written but were being printed from the
calling code in the less helpful Debug representation anyway.

Before:
    [ERROR:src/main.rs:705] The architecture failed to build the vm: NoVarEmpty

After:
    [ERROR:src/main.rs:705] The architecture failed to build the vm: /var/empty doesn't exist, can't jail devices.

TEST=cargo check --all-features
TEST=FEATURES=test emerge-amd64-generic crosvm

Change-Id: I77122c7d6861b2d610de2fff718896918ab21e10
Reviewed-on: https://chromium-review.googlesource.com/1469225
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Diffstat (limited to 'vhost')
-rw-r--r--vhost/src/lib.rs19
1 files changed, 18 insertions, 1 deletions
diff --git a/vhost/src/lib.rs b/vhost/src/lib.rs
index 552f546..f980bd9 100644
--- a/vhost/src/lib.rs
+++ b/vhost/src/lib.rs
@@ -14,6 +14,7 @@ pub use net::Net;
 pub use net::NetT;
 pub use vsock::Vsock;
 
+use std::fmt::{self, Display};
 use std::io::Error as IoError;
 use std::mem;
 use std::os::unix::io::AsRawFd;
@@ -41,6 +42,22 @@ pub enum Error {
 }
 pub type Result<T> = std::result::Result<T, Error>;
 
+impl Display for Error {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        use self::Error::*;
+
+        match self {
+            VhostOpen(e) => write!(f, "failed to open vhost device: {}", e),
+            IoctlError(e) => write!(f, "failed to run ioctl: {}", e),
+            InvalidQueue => write!(f, "invalid queue"),
+            DescriptorTableAddress(e) => write!(f, "invalid descriptor table address: {}", e),
+            UsedAddress(e) => write!(f, "invalid used address: {}", e),
+            AvailAddress(e) => write!(f, "invalid available address: {}", e),
+            LogAddress(e) => write!(f, "invalid log address: {}", e),
+        }
+    }
+}
+
 fn ioctl_result<T>() -> Result<T> {
     Err(Error::IoctlError(IoError::last_os_error()))
 }
@@ -331,7 +348,7 @@ mod tests {
             // FakeNet won't respond to ioctl's
             Ok(_t) => {}
             Err(Error::IoctlError(ref ioe)) if ioe.raw_os_error().unwrap() == 25 => {}
-            Err(e) => panic!("Unexpected Error:\n{:?}", e),
+            Err(e) => panic!("Unexpected Error:\n{}", e),
         }
     }