Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix crash when image loading fails due to network error",
"packageName": "react-native-windows",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,11 @@ ImageResponseOrImageErrorInfo ImageFailedResponse::ResolveImage() {
if (imageOrError.errorInfo->error.empty()) {
imageOrError.errorInfo->error = "Failed to load image.";
}
for (auto &&[header, value] : m_responseHeaders) {
imageOrError.errorInfo->httpResponseHeaders.push_back(
std::make_pair<std::string, std::string>(winrt::to_string(header), winrt::to_string(value)));
if (m_responseHeaders) {
for (auto &&[header, value] : m_responseHeaders) {
imageOrError.errorInfo->httpResponseHeaders.push_back(
std::make_pair<std::string, std::string>(winrt::to_string(header), winrt::to_string(value)));
}
}
return imageOrError;
}
Expand Down
14 changes: 13 additions & 1 deletion vnext/Microsoft.ReactNative/Fabric/WindowsImageManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <Fabric/Composition/ImageResponseImage.h>
#include <Fabric/Composition/UriImageManager.h>
#include <Networking/NetworkPropertyIds.h>
#include <Utils/CppWinrtLessExceptions.h>
#include <Utils/ImageUtils.h>
#include <fmt/format.h>
#include <functional/functor.h>
Expand Down Expand Up @@ -131,7 +132,18 @@ WindowsImageManager::GetImageRandomAccessStreamAsync(
request.Content(bodyContent);
}

winrt::Windows::Web::Http::HttpResponseMessage response(co_await m_httpClient.SendRequestAsync(request));
auto asyncOp = m_httpClient.SendRequestAsync(request);
co_await lessthrow_await_adapter<winrt::Windows::Foundation::IAsyncOperationWithProgress<
winrt::Windows::Web::Http::HttpResponseMessage,
winrt::Windows::Web::Http::HttpProgress>>{asyncOp};

if (asyncOp.Status() == winrt::Windows::Foundation::AsyncStatus::Error ||
asyncOp.Status() == winrt::Windows::Foundation::AsyncStatus::Canceled) {
co_return winrt::Microsoft::ReactNative::Composition::ImageFailedResponse(
L"Network request failed: A connection with the server could not be established.");
}

winrt::Windows::Web::Http::HttpResponseMessage response = asyncOp.GetResults();

if (!response.IsSuccessStatusCode()) {
co_return winrt::Microsoft::ReactNative::Composition::ImageFailedResponse(
Expand Down
Loading