use arcbox_error::CommonError;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, EngineError>;
#[derive(Debug, Error)]
pub enum EngineError {
#[error(transparent)]
Common(#[from] CommonError),
#[error("VMM error: {0}")]
Vmm(#[from] arcbox_vmm::VmmError),
#[error("snapshot error: {0}")]
Snapshot(#[from] arcbox_vmm::SnapshotError),
#[error("VM error: {0}")]
Vm(String),
#[error("machine error: {0}")]
Machine(String),
#[error("{message}")]
Agent {
code: i32,
message: String,
},
#[error("{context}: {source}")]
Transport {
context: &'static str,
#[source]
source: arcbox_transport::error::TransportError,
},
#[error("network error: {0}")]
Net(#[from] arcbox_net::NetError),
#[error(transparent)]
Image(#[from] arcbox_image::ImageError),
#[error("persistence error: {0}")]
Persistence(#[from] toml::de::Error),
#[error("internal lock poisoned")]
LockPoisoned,
}
impl EngineError {
#[must_use]
pub fn config(msg: impl Into<String>) -> Self {
Self::Common(CommonError::config(msg))
}
#[must_use]
pub fn not_found(resource: impl Into<String>) -> Self {
Self::Common(CommonError::not_found(resource))
}
#[must_use]
pub fn already_exists(resource: impl Into<String>) -> Self {
Self::Common(CommonError::already_exists(resource))
}
#[must_use]
pub fn invalid_state(msg: impl Into<String>) -> Self {
Self::Common(CommonError::invalid_state(msg))
}
}
impl From<std::io::Error> for EngineError {
fn from(err: std::io::Error) -> Self {
Self::Common(CommonError::from(err))
}
}
impl From<arcbox_transport::error::TransportError> for EngineError {
fn from(source: arcbox_transport::error::TransportError) -> Self {
Self::Transport {
context: "guest-agent transport failed",
source,
}
}
}