-
Notifications
You must be signed in to change notification settings - Fork 74
[WSLC] Encapsulate backend errors in a typed enum for the Rust SDK #1045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Soham Das (SohamDas2021)
merged 2 commits into
main
from
user/sodas/794-wslc-typed-errors
Aug 26, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! Typed failure modes for the WSLc backend. | ||
| //! | ||
| //! WSLc previously built every failure as a free-text [`ScriptResponse`], which | ||
| //! left `failure_phase` at its `None` default. Because | ||
| //! `mxc_engine::dispatch::map_spawn_error` discriminates on exactly that field, | ||
| //! every WSLc failure reached the Rust SDK as an opaque `backend_error` β so a | ||
| //! caller could only tell a missing-WSL host from a rejected policy by parsing | ||
| //! the message. Each variant here attributes the failure to a lifecycle phase | ||
| //! instead. | ||
| //! | ||
| //! `Display` reproduces the pre-existing message verbatim and | ||
| //! [`WslcError::into_response`] fills the same fields `ScriptResponse::error` | ||
| //! did, so the text a user sees is unchanged; `failure_phase` is the only | ||
| //! addition. | ||
|
|
||
| use std::fmt; | ||
|
|
||
| use wxc_common::models::{FailurePhase, ScriptResponse}; | ||
|
|
||
| use crate::wslc_bindings::HRESULT; | ||
|
|
||
| /// A WSLc backend failure, tagged with the phase it is attributable to. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub(crate) enum WslcError { | ||
| /// A WSLc SDK call returned a failing `HRESULT`. | ||
| Sdk { | ||
| context: String, | ||
| hr: HRESULT, | ||
| sdk_msg: String, | ||
| }, | ||
| /// The SDK, or a WSL component it depends on, is missing on this host. | ||
| Unavailable(String), | ||
| /// The request cannot be honored as written β a policy or config rejection | ||
| /// that the same input will not get past on a retry. | ||
| Rejected(String), | ||
| /// Host-side bring-up failed before the container process started. | ||
| Host(String), | ||
| /// The container started, but the run could not be carried to a clean exit. | ||
| Runtime(String), | ||
| } | ||
|
|
||
| impl WslcError { | ||
| /// A failing SDK call. Mirrors the message the former `sdk_error` helper | ||
| /// built, including the `HRESULT` rendering. | ||
| 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(), | ||
| } | ||
| } | ||
|
|
||
| /// Lifecycle phase this failure is attributed to, so a caller can tell a | ||
| /// retryable launch failure from a rejection or an unusable host. | ||
| pub(crate) fn failure_phase(&self) -> FailurePhase { | ||
| match self { | ||
| // Host cannot run WSLc at all β callers may fall back to another tier. | ||
| WslcError::Unavailable(_) => FailurePhase::BackendUnavailable, | ||
| // Non-retryable preflight: the input itself has to change. | ||
| WslcError::Rejected(_) => FailurePhase::Rejected, | ||
| // The SDK call or host bring-up failed; generally worth retrying. | ||
| WslcError::Sdk { .. } | WslcError::Host(_) => FailurePhase::LaunchFailed, | ||
| // The container was up but the run broke. | ||
| WslcError::Runtime(_) => FailurePhase::PostLaunchFailed, | ||
| } | ||
| } | ||
|
|
||
| /// Render as a [`ScriptResponse`], preserving the exact message text the | ||
| /// untyped construction produced. | ||
| pub(crate) fn into_response(self) -> ScriptResponse { | ||
| let failure_phase = self.failure_phase(); | ||
| let msg = self.to_string(); | ||
| ScriptResponse { | ||
| exit_code: -1, | ||
| standard_err: msg.clone(), | ||
| error_message: msg, | ||
| failure_phase, | ||
| ..Default::default() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| /// The former helper, kept verbatim so the typed path can be proven to | ||
| /// produce byte-identical text. | ||
| 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() { | ||
| // 0x8007_0490 as a c_long is negative; the `as u32` cast in both the old | ||
| // and new renderings must agree on the printed form. | ||
| 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_matches_the_untyped_shape_apart_from_the_phase() { | ||
| let typed = WslcError::Rejected("bad path".to_string()).into_response(); | ||
| let untyped = ScriptResponse::error("bad path"); | ||
|
|
||
| assert_eq!(typed.exit_code, untyped.exit_code); | ||
| assert_eq!(typed.standard_err, untyped.standard_err); | ||
| assert_eq!(typed.error_message, untyped.error_message); | ||
| // The untyped helper left this at the default, which is the bug. | ||
| assert_eq!(untyped.failure_phase, FailurePhase::None); | ||
| assert_eq!(typed.failure_phase, FailurePhase::Rejected); | ||
| } | ||
|
|
||
| #[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 | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Folded into #843