diff --git a/Cargo.lock b/Cargo.lock index 0e2abd5f6a..b2b8be9301 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -347,6 +347,7 @@ dependencies = [ "protos", "remain", "tempfile", + "thiserror", "vm_memory", ] diff --git a/disk/Cargo.toml b/disk/Cargo.toml index 7182dafc6f..8c49b45dc8 100644 --- a/disk/Cargo.toml +++ b/disk/Cargo.toml @@ -17,6 +17,7 @@ libc = "*" protobuf = { version = "2.3", optional = true } remain = "*" tempfile = "*" +thiserror = "*" cros_async = { path = "../cros_async" } data_model = { path = "../data_model" } protos = { path = "../protos", optional = true } diff --git a/disk/src/disk.rs b/disk/src/disk.rs index bd1945d307..792075fe6d 100644 --- a/disk/src/disk.rs +++ b/disk/src/disk.rs @@ -3,7 +3,7 @@ // found in the LICENSE file. use std::cmp::min; -use std::fmt::{self, Debug, Display}; +use std::fmt::Debug; use std::fs::File; use std::io::{self, Read, Seek, SeekFrom, Write}; use std::sync::Arc; @@ -16,6 +16,7 @@ use base::{ use cros_async::Executor; use libc::EINVAL; use remain::sorted; +use thiserror::Error as ThisError; use vm_memory::GuestMemory; mod qcow; @@ -30,25 +31,42 @@ mod android_sparse; use android_sparse::{AndroidSparse, SPARSE_HEADER_MAGIC}; #[sorted] -#[derive(Debug)] +#[derive(ThisError, Debug)] pub enum Error { + #[error("failed to create block device: {0}")] BlockDeviceNew(base::Error), + #[error("requested file conversion not supported")] ConversionNotSupported, + #[error("failure in android sparse disk: {0}")] CreateAndroidSparseDisk(android_sparse::Error), #[cfg(feature = "composite-disk")] + #[error("failure in composite disk: {0}")] CreateCompositeDisk(composite::Error), + #[error("failure creating single file disk: {0}")] CreateSingleFileDisk(cros_async::AsyncError), + #[error("failure with fallocate: {0}")] Fallocate(cros_async::AsyncError), + #[error("failure with fsync: {0}")] Fsync(cros_async::AsyncError), + #[error("failure in qcow: {0}")] QcowError(qcow::Error), + #[error("failed to read data: {0}")] ReadingData(io::Error), + #[error("failed to read header: {0}")] ReadingHeader(io::Error), + #[error("failed to read to memory: {0}")] ReadToMem(cros_async::AsyncError), + #[error("failed to seek file: {0}")] SeekingFile(io::Error), + #[error("failed to set file size: {0}")] SettingFileSize(io::Error), + #[error("unknown disk type")] UnknownType, + #[error("failed to write from memory: {0}")] WriteFromMem(cros_async::AsyncError), + #[error("failed to write from vec: {0}")] WriteFromVec(cros_async::AsyncError), + #[error("failed to write data: {0}")] WritingData(io::Error), } @@ -116,35 +134,6 @@ impl ToAsyncDisk for File { } } -impl Display for Error { - #[remain::check] - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::Error::*; - - #[sorted] - match self { - BlockDeviceNew(e) => write!(f, "failed to create block device: {}", e), - ConversionNotSupported => write!(f, "requested file conversion not supported"), - CreateAndroidSparseDisk(e) => write!(f, "failure in android sparse disk: {}", e), - #[cfg(feature = "composite-disk")] - CreateCompositeDisk(e) => write!(f, "failure in composite disk: {}", e), - CreateSingleFileDisk(e) => write!(f, "failure creating single file disk: {}", e), - Fallocate(e) => write!(f, "failure with fallocate: {}", e), - Fsync(e) => write!(f, "failure with fsync: {}", e), - QcowError(e) => write!(f, "failure in qcow: {}", e), - ReadingData(e) => write!(f, "failed to read data: {}", e), - ReadingHeader(e) => write!(f, "failed to read header: {}", e), - ReadToMem(e) => write!(f, "failed to read to memory: {}", e), - SeekingFile(e) => write!(f, "failed to seek file: {}", e), - SettingFileSize(e) => write!(f, "failed to set file size: {}", e), - UnknownType => write!(f, "unknown disk type"), - WriteFromMem(e) => write!(f, "failed to write from memory: {}", e), - WriteFromVec(e) => write!(f, "failed to write from vec: {}", e), - WritingData(e) => write!(f, "failed to write data: {}", e), - } - } -} - /// The variants of image files on the host that can be used as virtual disks. #[derive(Debug, PartialEq, Eq)] pub enum ImageType {