Win32: surface the real error when opening a URL fails#36
Open
bkaradzic-microsoft wants to merge 2 commits into
Open
Win32: surface the real error when opening a URL fails#36bkaradzic-microsoft wants to merge 2 commits into
bkaradzic-microsoft wants to merge 2 commits into
Conversation
UrlRequest::Impl::Open constructs a WinRT Foundation::Uri, which throws a winrt::hresult_error (not a std::exception) when the input is not a valid, supported URL. Callers such as the JsRuntimeHost XMLHttpRequest polyfill only catch std::exception, so the real failure was collapsed into a generic `Unknown error opening URL` with no HRESULT or message -- making these failures impossible to triage. Convert the hresult_error into a std::runtime_error that includes the offending URL, the system message, and the HRESULT, so the polyfill's existing catch (const std::exception&) reports the real reason. Also stop silently discarding the hresult_error in SendAsync: keep the status-0 (client-side error) contract but record the message/HRESULT into StatusText so send-time failures are diagnosable too. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Improves diagnostics in the Windows (Win32) URL request backend by preserving WinRT hresult_error details (message + HRESULT) when URL parsing or send-time operations fail, so downstream consumers (notably JS XMLHttpRequest) can surface actionable failure reasons instead of generic “unknown” errors.
Changes:
- Wrap WinRT
Foundation::Uriconstruction inUrlRequest::Impl::Openwith awinrt::hresult_errorcatch that rethrows asstd::runtime_errorcontaining the URL, message, and HRESULT. - Update Win32
SendAsyncto capture WinRT exception details intom_statusText(keeping the status code 0 contract) instead of discarding them.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Source/UrlRequest_Windows_Shared.h | Converts WinRT URI-construction failures into std::runtime_error with detailed context (URL + message + HRESULT). |
| Source/UrlRequest_Win32.cpp | Preserves WinRT send-time failure details by writing message + HRESULT into StatusText rather than swallowing the exception silently. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…Text Address review feedback: transport-level failures should be recorded through the public SetError API (ErrorString/ErrorSymbol/ErrorCode), consistent with the curl (Unix) and NSURL (Apple) backends, rather than written into StatusText -- which is documented as the HTTP reason phrase. The status code still stays 0 (client-side error), preserving the existing contract. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment on lines
+67
to
+69
| std::ostringstream message; | ||
| message << "Unable to open URL '" << url << "': " << winrt::to_string(error.message()) | ||
| << " (HRESULT 0x" << std::hex << std::uppercase << static_cast<uint32_t>(error.code()) << ")"; |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
UrlRequest::Impl::Open(Windows backend) constructs a WinRTFoundation::Uri:m_uri = Foundation::Uri{winrt::to_hstring(url)};When the input is not a valid/supported URL,
Foundation::Urithrows awinrt::hresult_error, which is not astd::exception. Downstream consumers such as the JsRuntimeHostXMLHttpRequestpolyfill only catchstd::exception, so the error falls through to a genericcatch (...)and is reported as an opaque:There is no message and no HRESULT, so these failures are impossible to triage from the JS side.
SendAsync(Win32) had the same problem in the other direction — it swallowedwinrt::hresult_errorentirely (catch (winrt::hresult_error) { ... }), discarding the message and HRESULT.Fix
UrlRequest_Windows_Shared.h(Impl::Open) — wrap theFoundation::Uriconstruction in atry/catchthat converts thewinrt::hresult_errorinto astd::runtime_errorincluding the offending URL, the system message, and the HRESULT. This routes through the polyfill's existingcatch (const std::exception&)so JS reports the real reason.UrlRequest_Win32.cpp(SendAsync) — keep the status-0 (client-side error) contract, but record the WinRT message + HRESULT intoStatusTextinstead of discarding it, so send-time failures are diagnosable viaxhr.statusText.No behavioral change on the success path; this only improves error reporting.
Real-world impact
While triaging a batch of Babylon Native asset-loading failures that all reported the useless
Unknown error opening URL, this change immediately surfaced the true cause. For example:The "URL" is actually a JavaScript dynamic-
importstatement being passed toXHR.open()— an entirely different problem than a network/transport error, and one that was completely hidden before this change.Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com