-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorFallback.js
More file actions
executable file
·205 lines (188 loc) · 4.57 KB
/
Copy pathErrorFallback.js
File metadata and controls
executable file
·205 lines (188 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* Error Fallback Component
* This component is displayed when there's a critical error during app initialization
*/
// CRITICAL FIX: Use require instead of import to avoid Hermes issues
const React = require('react');
const ReactNative = require('react-native');
const {
SafeAreaView,
StyleSheet,
Text,
View,
TouchableOpacity,
ScrollView,
Platform,
} = ReactNative;
class ErrorFallback extends React.Component {
constructor(props) {
super(props);
this.state = {
error: null,
errorInfo: null,
logs: [],
};
// Capture console logs
this.originalConsoleLog = console.log;
this.originalConsoleError = console.error;
this.originalConsoleWarn = console.warn;
console.log = (...args) => {
this.captureLog('log', args);
this.originalConsoleLog(...args);
};
console.error = (...args) => {
this.captureLog('error', args);
this.originalConsoleError(...args);
};
console.warn = (...args) => {
this.captureLog('warn', args);
this.originalConsoleWarn(...args);
};
}
captureLog(type, args) {
const log = {
type,
message: args.map(arg =>
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
).join(' '),
timestamp: new Date().toISOString(),
};
this.setState(prevState => ({
logs: [...prevState.logs.slice(-50), log], // Keep only the last 50 logs
}));
}
componentDidCatch(error, errorInfo) {
this.setState({
error,
errorInfo,
});
}
handleRestart = () => {
// Attempt to reload the app
if (Platform.OS === 'web') {
window.location.reload();
} else {
// For native platforms, we can only suggest restarting the app
alert('Please close and restart the app manually.');
}
};
render() {
return (
<SafeAreaView style={styles.container}>
<View style={styles.header}>
<Text style={styles.title}>DarkGridAI</Text>
</View>
<View style={styles.errorContainer}>
<Text style={styles.errorTitle}>Initialization Error</Text>
<Text style={styles.errorMessage}>
The app encountered an error during initialization. This is likely due to a native module loading issue.
</Text>
<Text style={styles.errorDetails}>
Error: {this.state.error ? this.state.error.toString() : 'Unknown error'}
</Text>
<TouchableOpacity style={styles.button} onPress={this.handleRestart}>
<Text style={styles.buttonText}>Restart App</Text>
</TouchableOpacity>
</View>
<View style={styles.logsContainer}>
<Text style={styles.logsTitle}>Debug Logs:</Text>
<ScrollView style={styles.logs}>
{this.state.logs.map((log, index) => (
<Text
key={index}
style={[
styles.logEntry,
log.type === 'error' && styles.errorLog,
log.type === 'warn' && styles.warnLog,
]}
>
{log.message}
</Text>
))}
</ScrollView>
</View>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f8f9fa',
},
header: {
padding: 16,
backgroundColor: '#4a6da7',
alignItems: 'center',
},
title: {
fontSize: 20,
fontWeight: 'bold',
color: 'white',
},
errorContainer: {
padding: 20,
alignItems: 'center',
},
errorTitle: {
fontSize: 22,
fontWeight: 'bold',
color: '#dc3545',
marginBottom: 10,
},
errorMessage: {
fontSize: 16,
color: '#343a40',
textAlign: 'center',
marginBottom: 20,
},
errorDetails: {
fontSize: 14,
color: '#721c24',
backgroundColor: '#f8d7da',
padding: 10,
borderRadius: 5,
marginBottom: 20,
width: '100%',
},
button: {
backgroundColor: '#007bff',
paddingHorizontal: 20,
paddingVertical: 10,
borderRadius: 5,
marginTop: 10,
},
buttonText: {
color: '#ffffff',
fontSize: 16,
fontWeight: 'bold',
},
logsContainer: {
flex: 1,
padding: 10,
backgroundColor: '#343a40',
},
logsTitle: {
color: '#ffffff',
fontSize: 16,
fontWeight: 'bold',
marginBottom: 10,
},
logs: {
flex: 1,
},
logEntry: {
color: '#cccccc',
fontSize: 12,
fontFamily: 'monospace',
marginBottom: 2,
},
errorLog: {
color: '#ff6b6b',
},
warnLog: {
color: '#ffd166',
},
});
// Export the component using CommonJS
module.exports = ErrorFallback;