Skip to content

Commit 92d8c80

Browse files
authored
[Fix crash] Image loading fails due to network error for instance shut down metro while app is running (#15428)
* Fix crash when image loading fails due to network error * Change files * add generic error message for network request failure * update yarn lock * update lockfile * update missing packages and upgrade react native platform override to latest canary
1 parent 4bbbdc8 commit 92d8c80

File tree

6 files changed

+423
-348
lines changed

6 files changed

+423
-348
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": "74712637+iamAbhi-916@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"fast-glob": "^3.2.11",
4545
"husky": "^4.2.5",
4646
"prettier-plugin-hermes-parser": "0.21.1",
47-
"react-native-platform-override": "0.0.0-canary.1015",
47+
"react-native-platform-override": "0.0.0-canary.1016",
4848
"unbroken": "1.0.27",
4949
"lage": "^2.7.1",
5050
"lodash": "^4.17.15"

packages/@rnw-scripts/promote-release/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@react-native-windows/package-utils": "^0.0.0-canary.96",
2525
"@typescript-eslint/eslint-plugin": "^7.1.1",
2626
"@typescript-eslint/parser": "^7.1.1",
27+
"minimatch": "^10.0.3",
2728
"chalk": "^4.1.0",
2829
"simple-git": "^3.3.0",
2930
"source-map-support": "^0.5.19",

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)