Skip to content

Commit 2bd169b

Browse files
feat: add error boundary demo
1 parent a7182f9 commit 2bd169b

3 files changed

Lines changed: 114 additions & 11 deletions

File tree

ExpoDemo/app/index.tsx

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,63 @@
1+
import { useState } from "react";
12
import { Button, Text, View } from "react-native";
2-
import RaygunClient, { LogLevel, RaygunClientOptions } from "raygun4reactnative";
3+
import RaygunClient, {
4+
LogLevel,
5+
RaygunClientOptions,
6+
RaygunErrorBoundary,
7+
} from "raygun4reactnative";
38

4-
export default function Index() {
9+
const options: RaygunClientOptions = {
10+
apiKey: "INSERT_YOUR_API_KEY_HERE",
11+
version: "0.1.2",
12+
enableCrashReporting: true,
13+
logLevel: LogLevel.verbose,
14+
};
15+
16+
RaygunClient.init(options);
17+
18+
function Crasher() {
19+
const [shouldCrash, setShouldCrash] = useState(false);
520

6-
const options: RaygunClientOptions = {
7-
apiKey: "INSERT_YOUR_API_KEY_HERE",
8-
version: "0.1.2",
9-
enableCrashReporting: true,
10-
logLevel: LogLevel.verbose,
11-
};
12-
13-
RaygunClient.init(options);
21+
if (shouldCrash) {
22+
throw new Error("Render-time error from Expo demo");
23+
}
1424

25+
return (
26+
<Button
27+
title="Trigger render error"
28+
onPress={() => setShouldCrash(true)}
29+
/>
30+
);
31+
}
32+
33+
export default function Index() {
1534
return (
1635
<View
1736
style={{
1837
flex: 1,
1938
justifyContent: "center",
2039
alignItems: "center",
40+
gap: 16,
2141
}}
2242
>
2343
<Text>Raygun Demo</Text>
24-
<Button title="Send Error" onPress={() => RaygunClient.sendError(Error("Error from Expo app"))} />
44+
<Button
45+
title="Send Error"
46+
onPress={() => RaygunClient.sendError(Error("Error from Expo app"))}
47+
/>
48+
49+
<RaygunErrorBoundary
50+
tags={["demo:expo"]}
51+
fallback={({ error, reset }) => (
52+
<View style={{ alignItems: "center", gap: 8 }}>
53+
<Text>Caught by RaygunErrorBoundary:</Text>
54+
<Text>{error.message}</Text>
55+
<Button title="Reset" onPress={reset} />
56+
</View>
57+
)}
58+
>
59+
<Crasher />
60+
</RaygunErrorBoundary>
2561
</View>
2662
);
2763
}

demo/App.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
44
import Home from "./screens/Home";
55
import CrashReporting from "./screens/CrashReporting";
66
import RealUserMonitoring from "./screens/RealUserMonitoring";
7+
import ErrorBoundaryScreen from "./screens/ErrorBoundary";
78
import {raygunClient} from "./utils/Utils";
89
import {LogLevel, RaygunClientOptions} from "raygun4reactnative";
910

@@ -48,6 +49,13 @@ function Tabs() {
4849
tabBarLabel: 'RUM'
4950
}}
5051
/>
52+
<Tab.Screen
53+
name="ErrorBoundary"
54+
component={ErrorBoundaryScreen}
55+
options={{
56+
tabBarLabel: 'Boundary'
57+
}}
58+
/>
5159
</Tab.Navigator>
5260
);
5361
}

demo/screens/ErrorBoundary.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React, {useState} from "react";
2+
import {Button, SafeAreaView, ScrollView, Text, View} from "react-native";
3+
import {RaygunErrorBoundary} from "raygun4reactnative";
4+
import {styles} from "../utils/Utils";
5+
6+
function Crasher() {
7+
const [shouldCrash, setShouldCrash] = useState(false);
8+
9+
if (shouldCrash) {
10+
throw new Error("Test Error: render-time error from ErrorBoundary demo");
11+
}
12+
13+
return (
14+
<Button
15+
title={"Trigger render error"}
16+
color={"red"}
17+
onPress={() => setShouldCrash(true)}
18+
/>
19+
);
20+
}
21+
22+
export default function ErrorBoundaryScreen() {
23+
return (
24+
<SafeAreaView>
25+
<ScrollView style={styles.scrollView}>
26+
<View style={styles.mainView}>
27+
<View style={styles.secondView}>
28+
<Text style={styles.title}>RaygunErrorBoundary</Text>
29+
<Text style={styles.subtitle}>
30+
Render-time errors thrown inside the boundary are reported to
31+
Raygun (with React `componentStack` as custom data) and the
32+
fallback UI is shown until reset.
33+
</Text>
34+
</View>
35+
36+
<View style={styles.secondView}>
37+
<RaygunErrorBoundary
38+
tags={["demo:error-boundary"]}
39+
customData={{screen: "ErrorBoundary"}}
40+
onError={(error) => {
41+
console.log("[ErrorBoundary] caught:", error.message);
42+
}}
43+
fallback={({error, reset}) => (
44+
<View style={{alignItems: "center"}}>
45+
<Text style={styles.subtitle}>
46+
Caught: {error.message}
47+
</Text>
48+
<Button title={"Reset"} color={"green"} onPress={reset} />
49+
</View>
50+
)}
51+
>
52+
<Crasher />
53+
</RaygunErrorBoundary>
54+
</View>
55+
</View>
56+
</ScrollView>
57+
</SafeAreaView>
58+
);
59+
}

0 commit comments

Comments
 (0)