Improve errors returned from WSLCContainer#40147
Improve errors returned from WSLCContainer#40147OneBlue wants to merge 5 commits intofeature/wsl-for-appsfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves WSLC container error handling by introducing container-specific HRESULTs and localized user-facing messages, enabling the CLI to treat certain state-related errors as non-fatal while preserving strict error semantics for the SDK.
Changes:
- Add new container-state HRESULTs (
WSLC_E_CONTAINER_NOT_RUNNING,WSLC_E_CONTAINER_IS_RUNNING) and wire them into error-string mapping. - Update
WSLCContaineroperations to throw localized “user error” messages for invalid state transitions. - Expand unit/e2e tests to validate new error codes/messages and CLI “no-op” behavior for already-stopped/started containers.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| test/windows/WSLCTests.cpp | Updates assertions to expect new HRESULTs and validates COM error messages. |
| test/windows/wslc/e2e/WSLCE2EContainerStopTests.cpp | Adds e2e coverage for container stop on an already-stopped container. |
| test/windows/wslc/e2e/WSLCE2EContainerCreateTests.cpp | Adds e2e coverage for container start on an already-running container. |
| src/windows/wslcsession/WSLCContainer.cpp | Emits new container-state HRESULTs and localized user messages from container APIs. |
| src/windows/wslc/services/ContainerService.cpp | Treats “already running/not running” as acceptable for CLI start/stop. |
| src/windows/service/inc/wslc.idl | Defines the new WSLC container-state HRESULTs. |
| src/windows/common/wslutil.cpp | Adds the new HRESULTs to common error string mapping. |
| src/shared/inc/defs.h | Introduces THROW_IF_FAILED_EXCEPT helper for selective HRESULT ignoring. |
| localization/strings/en-US/Resources.resw | Adds localized strings for the new container-state user error messages. |
| auto _result = (result); \ | ||
| if (FAILED(_result) && _result != (accepted)) \ | ||
| { \ | ||
| THROW_HR(_result); \ |
There was a problem hiding this comment.
THROW_IF_FAILED_EXCEPT suppresses an HRESULT but does not clear COM IErrorInfo that may have been set by the failed call. If this macro is used around COM calls (as in ContainerService), the thread can retain a stale error description/source and accidentally surface it for a later, unrelated failure. Consider explicitly clearing error info (e.g., SetErrorInfo(0, nullptr) or consuming it via GetErrorInfo) when _result == accepted, or documenting/encoding that this macro is COM-safe only when it clears error info.
| auto _result = (result); \ | |
| if (FAILED(_result) && _result != (accepted)) \ | |
| { \ | |
| THROW_HR(_result); \ | |
| const auto _result = (result); \ | |
| const auto _accepted = (accepted); \ | |
| if (FAILED(_result)) \ | |
| { \ | |
| if (_result == _accepted) \ | |
| { \ | |
| ::SetErrorInfo(0, nullptr); \ | |
| } \ | |
| else \ | |
| { \ | |
| THROW_HR(_result); \ | |
| } \ |
| THROW_HR_WITH_USER_ERROR_MSG( | ||
| WSLC_E_CONTAINER_NOT_RUNNING, | ||
| Localization::MessageWslcContainerNotRunning(m_id), |
There was a problem hiding this comment.
THROW_HR_WITH_USER_ERROR_MSG always inserts an extra ". " between the user-facing message and the internal detail. Since MessageWslcContainerNotRunning already ends with a period, the resulting internal exception text becomes "...not running.. Cannot stop..." (double period) which is noisy in logs/diagnostics. Consider removing trailing punctuation from the localized string, or avoid the auto-inserted period by using a non-suffixed user message for the _MSG form.
| THROW_HR_WITH_USER_ERROR_MSG( | |
| WSLC_E_CONTAINER_NOT_RUNNING, | |
| Localization::MessageWslcContainerNotRunning(m_id), | |
| auto userMessage = Localization::MessageWslcContainerNotRunning(m_id); | |
| if (!userMessage.empty() && userMessage.back() == L'.') | |
| { | |
| userMessage.pop_back(); | |
| } | |
| THROW_HR_WITH_USER_ERROR_MSG( | |
| WSLC_E_CONTAINER_NOT_RUNNING, | |
| userMessage, |
Summary of the Pull Request
This change introduces proper error messages and specialized error codes for errors coming out of WSLCContainers.
The specialized error codes allow wslc.exe to ignore some specific errors (like starting a container that's already running) while still allowing the SDK to return errors for invalid container state changes
PR Checklist
Detailed Description of the Pull Request / Additional comments
Validation Steps Performed