use std::fmt;
use crate::mxc_common::models::{FailurePhase, ScriptResponse};
use crate::mxc_common::mxc_error::ApiFailure;
use crate::wslc_common::wslc_bindings::HRESULT;
#[derive(Debug)]
pub struct WslcStepError {
pub failure_phase: FailurePhase,
pub error_message: String,
pub api_failure: Option<Box<ApiFailure>>,
}
impl WslcStepError {
pub fn with_api_failure(mut self, failure: ApiFailure) -> Self {
self.api_failure = Some(Box::new(failure));
self
}
pub fn into_response(self) -> ScriptResponse {
ScriptResponse::failed(self.failure_phase, &self.error_message)
}
}
impl From<ScriptResponse> for WslcStepError {
fn from(response: ScriptResponse) -> Self {
Self {
failure_phase: response.failure_phase,
error_message: response.error_message,
api_failure: None,
}
}
}
impl From<WslcStepError> for ScriptResponse {
fn from(error: WslcStepError) -> Self {
error.into_response()
}
}
pub(crate) fn sdk_api_failure(operation: &str, hr: HRESULT) -> ApiFailure {
ApiFailure::new(operation).with_native_code(format!("{:#010x}", hr as u32))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum WslcError {
Sdk {
context: String,
hr: HRESULT,
sdk_msg: String,
},
Unavailable(String),
Rejected(String),
Host(String),
Runtime(String),
}
impl WslcError {
pub(crate) fn sdk(context: impl Into<String>, hr: HRESULT, sdk_msg: impl Into<String>) -> Self {
WslcError::Sdk {
context: context.into(),
hr,
sdk_msg: sdk_msg.into(),
}
}
pub(crate) fn failure_phase(&self) -> FailurePhase {
match self {
WslcError::Unavailable(_) => FailurePhase::BackendUnavailable,
WslcError::Rejected(_) => FailurePhase::Rejected,
WslcError::Sdk { .. } | WslcError::Host(_) => FailurePhase::LaunchFailed,
WslcError::Runtime(_) => FailurePhase::PostLaunchFailed,
}
}
pub(crate) fn into_step_error(self) -> WslcStepError {
WslcStepError {
failure_phase: self.failure_phase(),
error_message: self.to_string(),
api_failure: None,
}
}
pub(crate) fn into_response(self) -> ScriptResponse {
ScriptResponse::failed(self.failure_phase(), &self.to_string())
}
}
impl fmt::Display for WslcError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
WslcError::Sdk {
context,
hr,
sdk_msg,
} if sdk_msg.is_empty() => {
write!(f, "{}: HRESULT 0x{:08X}", context, *hr as u32)
}
WslcError::Sdk {
context,
hr,
sdk_msg,
} => write!(f, "{}: {} (HRESULT 0x{:08X})", context, sdk_msg, *hr as u32),
WslcError::Unavailable(m)
| WslcError::Rejected(m)
| WslcError::Host(m)
| WslcError::Runtime(m) => f.write_str(m),
}
}
}
impl From<WslcError> for ScriptResponse {
fn from(err: WslcError) -> Self {
err.into_response()
}
}
impl From<WslcError> for WslcStepError {
fn from(err: WslcError) -> Self {
err.into_step_error()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn legacy_sdk_message(context: &str, hr: HRESULT, sdk_msg: &str) -> String {
if sdk_msg.is_empty() {
format!("{}: HRESULT 0x{:08X}", context, hr as u32)
} else {
format!("{}: {} (HRESULT 0x{:08X})", context, sdk_msg, hr as u32)
}
}
#[test]
fn sdk_message_is_unchanged_from_the_untyped_helper() {
for (ctx, hr, sdk_msg) in [
("WslcCreateContainer failed", 0x8007_0490_u32 as HRESULT, ""),
(
"WslcCreateContainerProcess failed",
0x8007_0490_u32 as HRESULT,
"no such image",
),
("WslcGetMissingComponents failed", -1, ""),
] {
assert_eq!(
WslcError::sdk(ctx, hr, sdk_msg).to_string(),
legacy_sdk_message(ctx, hr, sdk_msg),
"typed rendering must match the legacy message exactly"
);
}
}
#[test]
fn message_carrying_variants_render_verbatim() {
let msg = "WSLc: network.allowLocalNetwork=true is not supported.";
for err in [
WslcError::Unavailable(msg.to_string()),
WslcError::Rejected(msg.to_string()),
WslcError::Host(msg.to_string()),
WslcError::Runtime(msg.to_string()),
] {
assert_eq!(err.to_string(), msg, "message must not be reworded");
}
}
#[test]
fn response_carries_the_message_and_the_phase() {
let typed = WslcError::Host("bring-up failed".to_string()).into_response();
assert_eq!(typed.exit_code, -1);
assert_eq!(typed.error_message, "bring-up failed");
assert_eq!(typed.failure_phase, FailurePhase::LaunchFailed);
}
#[test]
fn a_step_error_becomes_the_response_create_and_run_returns() {
let hr = 0x8007_2740_u32 as HRESULT;
let error = WslcError::sdk("WslcStartContainer failed", hr, "collision");
let create_and_run = error.clone().into_response();
let step = error
.into_step_error()
.with_api_failure(sdk_api_failure("WslcStartContainer", hr));
assert_eq!(
serde_json::to_value(step.into_response()).unwrap(),
serde_json::to_value(create_and_run).unwrap()
);
}
#[test]
fn exit_codes_follow_the_shared_phase_contract() {
let s = || "x".to_string();
assert_eq!(
WslcError::Rejected(s()).into_response().exit_code,
ScriptResponse::rejected("x").exit_code
);
assert_eq!(
WslcError::Unavailable(s()).into_response().exit_code,
ScriptResponse::unavailable("x").exit_code
);
for err in [
WslcError::Host(s()),
WslcError::Runtime(s()),
WslcError::sdk("op", -1, ""),
] {
assert_eq!(err.into_response().exit_code, -1);
}
}
#[test]
fn each_variant_maps_to_its_lifecycle_phase() {
let s = || "x".to_string();
assert_eq!(
WslcError::Unavailable(s()).failure_phase(),
FailurePhase::BackendUnavailable
);
assert_eq!(
WslcError::Rejected(s()).failure_phase(),
FailurePhase::Rejected
);
assert_eq!(
WslcError::sdk("op", -1, "").failure_phase(),
FailurePhase::LaunchFailed
);
assert_eq!(
WslcError::Host(s()).failure_phase(),
FailurePhase::LaunchFailed
);
assert_eq!(
WslcError::Runtime(s()).failure_phase(),
FailurePhase::PostLaunchFailed
);
}
}