Skip to content

Commit 845c3e0

Browse files
authored
[0.79 Cherry pick] Image loading fails due to network error for instance shut down metro while app is running (#15449)
* Fix crash when image loading fails due to network error * Change files * add generic error message for network request failure
1 parent 2864adf commit 845c3e0

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Fix crash when image loading fails due to network error",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

vnext/Microsoft.ReactNative/Fabric/Composition/UriImageManager.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,11 @@ ImageResponseOrImageErrorInfo ImageFailedResponse::ResolveImage() {
291291
if (imageOrError.errorInfo->error.empty()) {
292292
imageOrError.errorInfo->error = "Failed to load image.";
293293
}
294-
for (auto &&[header, value] : m_responseHeaders) {
295-
imageOrError.errorInfo->httpResponseHeaders.push_back(
296-
std::make_pair<std::string, std::string>(winrt::to_string(header), winrt::to_string(value)));
294+
if (m_responseHeaders) {
295+
for (auto &&[header, value] : m_responseHeaders) {
296+
imageOrError.errorInfo->httpResponseHeaders.push_back(
297+
std::make_pair<std::string, std::string>(winrt::to_string(header), winrt::to_string(value)));
298+
}
297299
}
298300
return imageOrError;
299301
}

vnext/Microsoft.ReactNative/Fabric/WindowsImageManager.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <Fabric/Composition/ImageResponseImage.h>
1111
#include <Fabric/Composition/UriImageManager.h>
1212
#include <Networking/NetworkPropertyIds.h>
13+
#include <Utils/CppWinrtLessExceptions.h>
1314
#include <Utils/ImageUtils.h>
1415
#include <fmt/format.h>
1516
#include <functional/functor.h>
@@ -131,7 +132,19 @@ WindowsImageManager::GetImageRandomAccessStreamAsync(
131132
request.Content(bodyContent);
132133
}
133134

134-
winrt::Windows::Web::Http::HttpResponseMessage response(co_await m_httpClient.SendRequestAsync(request));
135+
auto asyncOp = m_httpClient.SendRequestAsync(request);
136+
co_await lessthrow_await_adapter<winrt::Windows::Foundation::IAsyncOperationWithProgress<
137+
winrt::Windows::Web::Http::HttpResponseMessage,
138+
winrt::Windows::Web::Http::HttpProgress>>{asyncOp};
139+
140+
if (asyncOp.Status() == winrt::Windows::Foundation::AsyncStatus::Error ||
141+
asyncOp.Status() == winrt::Windows::Foundation::AsyncStatus::Canceled) {
142+
auto errorMessage = FormatHResultError(winrt::hresult_error(asyncOp.ErrorCode()));
143+
co_return winrt::Microsoft::ReactNative::Composition::ImageFailedResponse(
144+
winrt::to_hstring("Network request failed: " + errorMessage));
145+
}
146+
147+
winrt::Windows::Web::Http::HttpResponseMessage response = asyncOp.GetResults();
135148

136149
if (!response.IsSuccessStatusCode()) {
137150
co_return winrt::Microsoft::ReactNative::Composition::ImageFailedResponse(

0 commit comments

Comments
 (0)