Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

add error boundry and exception handler

Ammar Ahmedb7c93b101969
diff --git a/apps/mobile/App.js b/apps/mobile/App.js
index 717483011..def180f7b 100644
--- a/apps/mobile/App.js
+++ b/apps/mobile/App.js
@@ -1,7 +1,11 @@
+import Clipboard from '@react-native-clipboard/clipboard';
import React, { useEffect } from 'react';
+import { ScrollView, Text, View } from 'react-native';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import Launcher from './src/components/launcher';
+import { Button } from './src/components/ui/button';
+import Paragraph from './src/components/ui/typography/paragraph';
import { ApplicationHolder } from './src/navigation';
import Notifications from './src/services/notifications';
import SettingsService from './src/services/settings';
@@ -9,6 +13,65 @@ import { TipManager } from './src/services/tip-manager';
import { useUserStore } from './src/stores/use-user-store';
import { useAppEvents } from './src/utils/hooks/use-app-events';
+class MyErrorBoundary extends React.Component {
+ state = {
+ hasError: false,
+ error: null
+ };
+
+ static getDerivedStateFromError(error) {
+ return { hasError: true, error: error };
+ }
+ componentDidCatch(error, errorInfo) {
+ // A custom error logging function
+ }
+ render() {
+ return this.state.hasError ? (
+ <View
+ style={{
+ flex: 1,
+ justifyContent: 'center',
+ alignItems: 'center',
+ paddingHorizontal: 12,
+ paddingVertical: 50
+ }}
+ >
+ <ScrollView
+ style={{
+ borderWidth: 1,
+ borderRadius: 10,
+ padding: 12,
+ borderColor: 'red'
+ }}
+ >
+ <Paragraph
+ style={{
+ color: 'red'
+ }}
+ selectable={true}
+ >
+ {this.state.error.stack}
+ </Paragraph>
+ </ScrollView>
+ <Button
+ title="Copy Error"
+ type="error"
+ style={{
+ marginTop: 20,
+ width: '100%'
+ }}
+ onPress={() => {
+ Clipboard.setString(this.state.error.stack);
+ alert('Error copied');
+ }}
+ />
+ </View>
+ ) : (
+ this.props.children
+ );
+ }
+}
+
SettingsService.init();
SettingsService.checkOrientation();
const App = () => {
@@ -27,16 +90,18 @@ const App = () => {
}, []);
return (
- <GestureHandlerRootView
- style={{
- flex: 1
- }}
- >
- <SafeAreaProvider>
- <ApplicationHolder />
- <Launcher />
- </SafeAreaProvider>
- </GestureHandlerRootView>
+ <MyErrorBoundary>
+ <GestureHandlerRootView
+ style={{
+ flex: 1
+ }}
+ >
+ <SafeAreaProvider>
+ <ApplicationHolder />
+ <Launcher />
+ </SafeAreaProvider>
+ </GestureHandlerRootView>
+ </MyErrorBoundary>
);
};
diff --git a/apps/mobile/globals.js b/apps/mobile/globals.js
index 3967d11c7..14404deb6 100644
--- a/apps/mobile/globals.js
+++ b/apps/mobile/globals.js
@@ -1,4 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */
+import { setJSExceptionHandler, getJSExceptionHandler } from 'react-native-exception-handler';
+
global.Buffer = require('buffer').Buffer;
//import { ScriptManager, Script } from '@callstack/repack/client';
import './src/utils/logger/index';
@@ -20,6 +22,14 @@ import { DOMParser } from './worker.js';
// }
global.DOMParser = DOMParser;
+//=================================================
+// ADVANCED use case:
+const exceptionhandler = (error, isFatal) => {
+ // your error handler function
+ alert('Exception Handler: ' + error.stack);
+};
+setJSExceptionHandler(exceptionhandler, true);
+
// try {
// const shared = ScriptManager.shared;
// } catch (e) {