@momo-kits/foundation 0.157.1-beta.2-viewboundary-test → 0.157.1-beta.3-viewboundary-test
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ViewBoundary/index.tsx +89 -2
- package/package.json +1 -1
package/ViewBoundary/index.tsx
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { ViewStyle, StyleProp } from 'react-native';
|
|
2
|
+
import { NativeModules, ViewStyle, StyleProp } from 'react-native';
|
|
3
|
+
import parseErrorStack from 'react-native/Libraries/Core/Devtools/parseErrorStack';
|
|
4
|
+
import { MiniAppContext } from '../Context';
|
|
5
|
+
|
|
6
|
+
const MiniAppModule = NativeModules.MiniAppModule;
|
|
7
|
+
const MomoSentry = NativeModules.MomoSentry;
|
|
8
|
+
|
|
9
|
+
const DELAY_TIME = 10 * 1000; // 10s - prevent duplicate reports
|
|
3
10
|
|
|
4
11
|
export interface ViewBoundaryProps {
|
|
5
12
|
children: React.ReactNode;
|
|
@@ -17,6 +24,10 @@ interface ViewBoundaryState {
|
|
|
17
24
|
}
|
|
18
25
|
|
|
19
26
|
class ViewBoundary extends React.Component<ViewBoundaryProps, ViewBoundaryState> {
|
|
27
|
+
static contextType = MiniAppContext;
|
|
28
|
+
static lastReportTime = 0;
|
|
29
|
+
static lastReportMessage = '';
|
|
30
|
+
|
|
20
31
|
constructor(props: ViewBoundaryProps) {
|
|
21
32
|
super(props);
|
|
22
33
|
this.state = { hasError: false, error: null };
|
|
@@ -28,6 +39,83 @@ class ViewBoundary extends React.Component<ViewBoundaryProps, ViewBoundaryState>
|
|
|
28
39
|
|
|
29
40
|
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
|
|
30
41
|
this.props.onError?.(error, errorInfo);
|
|
42
|
+
|
|
43
|
+
const context = (this as any).context ?? {};
|
|
44
|
+
const appId = context.appId ?? '';
|
|
45
|
+
const hostId = context.hostId ?? '';
|
|
46
|
+
|
|
47
|
+
// 1. Report to native MiniAppModule → EVENT_JS_RENDER_CRASH → M01 (RENDER_CRASH) / P01 (PLATFORM_RENDER_CRASH)
|
|
48
|
+
this.reportRenderCrash(error, errorInfo, appId, hostId);
|
|
49
|
+
|
|
50
|
+
// 2. Report to Sentry directly via MomoSentry → M05 (MINI_APP_GLOBAL_JS_EXCEPTION)
|
|
51
|
+
this.reportSentry(error, appId);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Flow M01/P01: MiniAppModule.onJSRenderCrash
|
|
56
|
+
* → Native dispatch EVENT_JS_RENDER_CRASH
|
|
57
|
+
* → RNContainerViewModel → tracker.trackMiniAppRenderFailed
|
|
58
|
+
* → Sentry with error code M01 (MiniApp) or P01 (Platform)
|
|
59
|
+
*/
|
|
60
|
+
private reportRenderCrash(error: Error, errorInfo: React.ErrorInfo, appId: string, hostId: string) {
|
|
61
|
+
try {
|
|
62
|
+
if (MiniAppModule?.onJSRenderCrash) {
|
|
63
|
+
MiniAppModule.onJSRenderCrash({
|
|
64
|
+
hostId,
|
|
65
|
+
appId,
|
|
66
|
+
uniqueId: appId,
|
|
67
|
+
error: {
|
|
68
|
+
uniqueId: appId,
|
|
69
|
+
errorName: error.name,
|
|
70
|
+
errorMessage: error.message,
|
|
71
|
+
componentStack: errorInfo?.componentStack,
|
|
72
|
+
isComponentError: true,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
} catch (_e) {
|
|
77
|
+
// Silently fail if native module not available
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Flow M05: MomoSentry.captureJSCrash
|
|
83
|
+
* → SentryJSException → SentryReport
|
|
84
|
+
* → Sentry with error code M05 (MINI_APP_GLOBAL_JS_EXCEPTION)
|
|
85
|
+
* Same pattern as AppCrash.reportJsException on platform
|
|
86
|
+
*/
|
|
87
|
+
private reportSentry(error: Error, appId: string) {
|
|
88
|
+
try {
|
|
89
|
+
if (__DEV__ || !MomoSentry?.captureJSCrash) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const { name, message = '', stack } = error;
|
|
94
|
+
if (!message || !stack) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Prevent duplicate reports (same logic as AppCrash.js)
|
|
99
|
+
if (
|
|
100
|
+
Date.now() - ViewBoundary.lastReportTime > DELAY_TIME &&
|
|
101
|
+
ViewBoundary.lastReportMessage !== message
|
|
102
|
+
) {
|
|
103
|
+
ViewBoundary.lastReportTime = Date.now();
|
|
104
|
+
ViewBoundary.lastReportMessage = message;
|
|
105
|
+
|
|
106
|
+
const errorStack = parseErrorStack(stack);
|
|
107
|
+
MomoSentry.captureJSCrash({
|
|
108
|
+
appId: appId || 'vn.momo.platform',
|
|
109
|
+
errorStack,
|
|
110
|
+
errorName: name,
|
|
111
|
+
errorMessage: message,
|
|
112
|
+
isFatal: false,
|
|
113
|
+
tag: 'ViewBoundary',
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
} catch (_e) {
|
|
117
|
+
// Silently fail
|
|
118
|
+
}
|
|
31
119
|
}
|
|
32
120
|
|
|
33
121
|
render() {
|
|
@@ -41,5 +129,4 @@ class ViewBoundary extends React.Component<ViewBoundaryProps, ViewBoundaryState>
|
|
|
41
129
|
}
|
|
42
130
|
}
|
|
43
131
|
|
|
44
|
-
export default ViewBoundary;
|
|
45
132
|
export { ViewBoundary };
|