-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorBoundary.tsx
More file actions
88 lines (78 loc) · 2.07 KB
/
ErrorBoundary.tsx
File metadata and controls
88 lines (78 loc) · 2.07 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
import React, { Component, ReactNode, ErrorInfo } from "react";
import { View, Text, Button, StyleSheet, ScrollView } from "react-native";
import { reportError } from "@/services/errorHandler.service";
interface Props {
children: ReactNode;
}
interface State {
hasError: boolean;
error: Error | null;
}
class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
// FIX: Calling reportError with the correct 'options' object.
reportError(error, { componentStack: 'ErrorBoundary', extra: { errorInfo } });
}
handleResetError = () => {
this.setState({ hasError: false, error: null });
};
render() {
if (this.state.hasError) {
return (
<View style={styles.container}>
<Text style={styles.title}>Oops! Something went wrong.</Text>
<Text style={styles.subtitle}>
Our team has been notified. Please try again.
</Text>
<ScrollView style={styles.errorContainer}>
<Text style={styles.errorText}>{this.state.error?.toString()}</Text>
</ScrollView>
<Button title="Try Again" onPress={this.handleResetError} />
</View>
);
}
return this.props.children;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
padding: 20,
backgroundColor: "#fefefe",
},
title: {
fontSize: 22,
fontWeight: "bold",
marginBottom: 15,
textAlign: "center",
color: "#333",
},
subtitle: {
fontSize: 16,
marginBottom: 20,
textAlign: "center",
color: "#555",
},
errorContainer: {
maxHeight: 200,
width: "100%",
backgroundColor: "#f0f0f0",
borderRadius: 8,
padding: 15,
marginBottom: 20,
},
errorText: {
color: "#d32f2f",
fontFamily: "monospace",
},
});
export default ErrorBoundary;