Skip to content

Commit e8d0404

Browse files
authored
Clean up error messages (#14695)
* Clean up error messages * Change files
1 parent 1e8e1a3 commit e8d0404

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
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": "Clean up error messages",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,9 @@ void ReactNativeIsland::UninitRootView() noexcept {
530530
if (m_context.Handle().LoadingState() == winrt::Microsoft::ReactNative::LoadingState::HasError)
531531
return;
532532

533-
auto uiManager = ::Microsoft::ReactNative::FabricUIManager::FromProperties(
534-
winrt::Microsoft::ReactNative::ReactPropertyBag(m_context.Properties()));
535-
uiManager->stopSurface(static_cast<facebook::react::SurfaceId>(RootTag()));
533+
if (auto uiManager = ::Microsoft::ReactNative::FabricUIManager::FromProperties(
534+
winrt::Microsoft::ReactNative::ReactPropertyBag(m_context.Properties())))
535+
uiManager->stopSurface(static_cast<facebook::react::SurfaceId>(RootTag()));
536536
}
537537

538538
m_rootTag = -1;

vnext/Microsoft.ReactNative/Fabric/FabricUIManagerModule.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ FabicUIManagerProperty() noexcept {
4646

4747
/*static*/ std::shared_ptr<FabricUIManager> FabricUIManager::FromProperties(
4848
const winrt::Microsoft::ReactNative::ReactPropertyBag &props) {
49-
return props.Get(FabicUIManagerProperty()).Value();
49+
auto p = props.Get(FabicUIManagerProperty());
50+
if (p)
51+
return p.Value();
52+
return {};
5053
}
5154

5255
FabricUIManager::FabricUIManager() {}

vnext/Microsoft.ReactNative/RedBox.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,36 @@ struct RedBox : public std::enable_shared_from_this<RedBox> {
8383
// Ideally we'd have a dialog we could push UI updates to. -- Maybe we could load XAML and host the XAML dialog?
8484

8585
std::stringstream ss;
86-
ss << m_errorInfo.Message;
86+
87+
const std::regex colorsRegex(
88+
"(\\x1b\\[[0-9;]*m)|(\\\\u001b\\[\\d*m)"); // strip out console colors which is often added to JS error messages
89+
auto message = std::regex_replace(m_errorInfo.Message, colorsRegex, "");
90+
bool needsMessage = true;
91+
92+
if (!message.empty() && message[0] == '{') {
93+
try {
94+
auto json = folly::parseJson(message);
95+
96+
if (json.count("type") && json["type"] == "TransformError") {
97+
ss << "TransformError: " << std::endl;
98+
if (json.count("errors")) {
99+
for (const auto &err : json["errors"]) {
100+
ss << "Filename: " << err["filename"] << std::endl;
101+
ss << "lineNumber: " << err["lineNumber"] << std::endl;
102+
ss << err["description"] << std::endl;
103+
}
104+
}
105+
needsMessage = false;
106+
} else {
107+
message = folly::toPrettyJson(json);
108+
message = std::regex_replace(m_errorInfo.Message, colorsRegex, "");
109+
}
110+
} catch (...) {
111+
}
112+
}
113+
114+
if (needsMessage)
115+
ss << message;
87116
ss << std::endl << std::endl;
88117
for (auto frame : m_errorInfo.Callstack) {
89118
ss << frame.Method << "\n" << frame.File << ":" << frame.Line << ":" << frame.Column << std::endl;

0 commit comments

Comments
 (0)