diff --git a/change/react-native-windows-4ab9e54e-d49e-49da-a4a1-f2a2d8218f94.json b/change/react-native-windows-4ab9e54e-d49e-49da-a4a1-f2a2d8218f94.json new file mode 100644 index 00000000000..f2523ff6d3d --- /dev/null +++ b/change/react-native-windows-4ab9e54e-d49e-49da-a4a1-f2a2d8218f94.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Catch and report errors in linking module", + "packageName": "react-native-windows", + "email": "asklar@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Microsoft.ReactNative/Modules/LinkingManagerModule.cpp b/vnext/Microsoft.ReactNative/Modules/LinkingManagerModule.cpp index 722eefd4b5b..fec8527f3a7 100644 --- a/vnext/Microsoft.ReactNative/Modules/LinkingManagerModule.cpp +++ b/vnext/Microsoft.ReactNative/Modules/LinkingManagerModule.cpp @@ -103,14 +103,24 @@ auto LinkingManagerModule::getMethods() -> std::vector { Method( "openURL", [](folly::dynamic args, Callback successCallback, Callback errorCallback) { - winrt::Windows::Foundation::Uri uri(Utf8ToUtf16(facebook::xplat::jsArgAsString(args, 0))); - openURLAsync(uri, successCallback, errorCallback); + auto inputUrl = facebook::xplat::jsArgAsString(args, 0); + try { + winrt::Windows::Foundation::Uri uri(Utf8ToUtf16(inputUrl)); + openURLAsync(uri, successCallback, errorCallback); + } catch (winrt::hresult_error &e) { + errorCallback( + {folly::dynamic::object("code", e.code().value)("message", "Unable to open URL: " + inputUrl)}); + } }), Method( "canOpenURL", [](folly::dynamic args, Callback successCallback, Callback errorCallback) { - winrt::Windows::Foundation::Uri uri(Utf8ToUtf16(facebook::xplat::jsArgAsString(args, 0))); - canOpenURLAsync(uri, successCallback, errorCallback); + try { + winrt::Windows::Foundation::Uri uri(Utf8ToUtf16(facebook::xplat::jsArgAsString(args, 0))); + canOpenURLAsync(uri, successCallback, errorCallback); + } catch (winrt::hresult_error &e) { + successCallback({false}); + } }), Method( "getInitialURL",