1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::{fmt, result};

pub type Result<T> = result::Result<T, Error>;

#[derive(Debug, Clone)]
pub enum Error {
	FileMissing,
	InternalError,
	OsError(i32),
	InvalidFile(String),
	NotEnoughMemory,
	MissingFrequency,
	UnknownExitReason,
	Shutdown,
	ParseMemory,
	UnhandledExitReason,
	InvalidMacAddress,
	ParseIntError,
	ParseRangeError,
	InvalidArgument(String),
	#[cfg(target_os = "macos")]
	Hypervisor(xhypervisor::Error),
}

#[cfg(target_os = "linux")]
pub fn to_error<T>(err: kvm_ioctls::Error) -> Result<T> {
	Err(Error::OsError(err.errno()))
}

#[cfg(target_os = "macos")]
impl From<xhypervisor::Error> for Error {
	fn from(err: xhypervisor::Error) -> Self {
		Error::Hypervisor(err)
	}
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match *self {
			Error::FileMissing => write!(f, "No execution file given"),
			Error::InternalError => write!(f, "An internal error has occurred, please report."),
			Error::OsError(ref err) => write!(f, "Error from OS: {}", err),
			Error::InvalidFile(ref file) => {
				write!(f, "The file {} was not found or is invalid.", file)
			}
			Error::NotEnoughMemory => write!(
				f,
				"The host system has not enough memory, please check your memory usage."
			),
			Error::MissingFrequency => write!(
				f,
				"Couldn't get the CPU frequency from your system. (is /proc/cpuinfo missing?)"
			),
			Error::UnknownExitReason => write!(f, "Unknown exit reason."),
			Error::Shutdown => write!(f, "Receives shutdown command"),
			Error::ParseMemory => write!(
				f,
				"Couldn't parse the guest memory size from the environment"
			),
			Error::UnhandledExitReason => write!(f, "Unhandled exit reason"),
			Error::InvalidMacAddress => write!(f, "Invalid MAC address"),
			Error::ParseIntError => write!(f, "Unable to parse string"),
			Error::ParseRangeError => write!(f, "Unable to parse string range"),
			Error::InvalidArgument(ref arg) => write!(f, "Invalid argument passed: {}", arg),
			#[cfg(target_os = "macos")]
			Error::Hypervisor(ref err) => write!(f, "The hypervisor has failed: {:?}", err),
		}
	}
}