use crate::mxc_common::models::{FailurePhase, ScriptResponse};
#[derive(Debug)]
pub(crate) enum OneShotError {
SandboxUnavailable(String),
Busy(String),
RuntimeSetup(String),
Launch(String),
Policy(String),
Exec(String),
}
impl OneShotError {
pub(crate) fn into_response(self) -> ScriptResponse {
let failure_phase = self.failure_phase();
let (summary, detail): (&str, String) = match self {
OneShotError::SandboxUnavailable(d) => (
"Windows Sandbox is not available on this host. Enable the \
'Containers-DisposableClientVM' optional feature and reboot.",
d,
),
OneShotError::Busy(d) => (
"Windows Sandbox is already running and was not started by a \
disposable MXC run. Refusing to start a one-shot sandbox \
(the host allows only one running instance). Close the \
existing sandbox and retry.",
d,
),
OneShotError::RuntimeSetup(d) => (
"Failed to initialise the async runtime for Windows Sandbox.",
d,
),
OneShotError::Launch(d) => (
"Failed to launch the Windows Sandbox VM or connect to the guest agent.",
d,
),
OneShotError::Policy(d) => (
"The request's policy cannot be enforced by the Windows Sandbox \
backend.",
d,
),
OneShotError::Exec(d) => ("Execution inside the Windows Sandbox failed.", d),
};
ScriptResponse {
standard_err: summary.to_string(),
extended_error: detail,
..ScriptResponse::failed(failure_phase, summary)
}
}
fn failure_phase(&self) -> FailurePhase {
match self {
OneShotError::SandboxUnavailable(_) => FailurePhase::BackendUnavailable,
OneShotError::Policy(_) => FailurePhase::Rejected,
OneShotError::Busy(_) | OneShotError::RuntimeSetup(_) | OneShotError::Launch(_) => {
FailurePhase::LaunchFailed
}
OneShotError::Exec(_) => FailurePhase::PostLaunchFailed,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mxc_common::mxc_error::MxcErrorCode;
#[test]
fn busy_maps_to_launch_failed_with_detail() {
let resp = OneShotError::Busy("vm already up".to_string()).into_response();
assert_eq!(resp.exit_code, -1);
assert_eq!(resp.failure_phase, FailurePhase::LaunchFailed);
assert_eq!(resp.extended_error, "vm already up");
assert!(!resp.error_message.is_empty());
}
#[test]
fn a_refused_policy_is_told_apart_from_a_host_that_cannot_run_it() {
let refused = OneShotError::Policy("denied path in share".to_string()).into_response();
assert_eq!(refused.failure_phase, FailurePhase::Rejected);
assert_eq!(refused.exit_code, 1);
assert_eq!(
refused.failure_phase.error_code(),
MxcErrorCode::PolicyValidation
);
let unavailable =
OneShotError::SandboxUnavailable("feature off".to_string()).into_response();
assert_eq!(unavailable.failure_phase, FailurePhase::BackendUnavailable);
assert_eq!(unavailable.exit_code, -1);
assert_eq!(
unavailable.failure_phase.error_code(),
MxcErrorCode::BackendUnavailable
);
}
#[test]
fn launch_and_exec_errors_map_to_post_launch_phases() {
assert_eq!(
OneShotError::Launch("rendezvous timed out after 360s".to_string())
.into_response()
.failure_phase,
FailurePhase::LaunchFailed
);
assert_eq!(
OneShotError::Exec("relay broke".to_string())
.into_response()
.failure_phase,
FailurePhase::PostLaunchFailed
);
}
#[test]
fn launch_detail_preserved_in_extended_error() {
let resp =
OneShotError::Launch("rendezvous timed out after 360s".to_string()).into_response();
assert_eq!(resp.extended_error, "rendezvous timed out after 360s");
assert!(resp.error_message.contains("launch"));
}
}