import React, { useState } from 'react'; import { Alert, Platform, ScrollView, StyleSheet, Text, View, Switch } from 'react-native'; import { CrashReporting, NonFatalErrorLevel } from 'instabug-reactnative'; import { ListTile } from '../components/ListTile'; import { Screen } from '../components/Screen'; import { Section } from '../components/Section'; import { PlatformListTile } from '../components/PlatformListTile'; import { NativeExampleCrashReporting } from '../native/NativeCrashReporting'; import { VerticalListTile } from '../components/VerticalListTile'; import { Button, VStack } from 'native-base'; import { InputField } from '../components/InputField'; import { Select } from '../components/Select'; import { showNotification } from '../utils/showNotification'; const styles = StyleSheet.create({ inputWrapper: { padding: 4, flex: 1, }, formContainer: { flexDirection: 'row', alignItems: 'stretch', }, }); export const CrashReportingScreen: React.FC = () => { function throwHandledException(error: Error) { try { if (!error.message) { const appName = 'Instabug Test App'; error.message = `Handled ${error.name} From ${appName}`; } throw error; } catch (err) { if (err instanceof Error) { CrashReporting.reportError(err, { level: NonFatalErrorLevel.critical }).then(() => Alert.alert(`Crash report for ${error.name} is Sent!`), ); } } } function throwUnhandledException(error: Error, isPromise: boolean = false) { const appName = 'Instabug Test App'; const rejectionType = isPromise ? 'Promise Rejection ' : ''; const errorMessage = `Unhandled ${rejectionType}${error.name} from ${appName}`; if (!error.message) { console.log(`IBG-CRSH | Error message: ${error.message}`); error.message = errorMessage; } if (isPromise) { console.log('IBG-CRSH | Promise'); Promise.reject(error).then(() => Alert.alert(`Promise Rejection Crash report for ${error.name} is Sent!`), ); } else { throw error; } } function throwUnhandledChainingException(error: Error, isPromise: boolean = false) { const appName = 'Instabug Test App'; const rejectionType = isPromise ? 'Promise Rejection ' : ''; const errorMessage = `Unhandled ${rejectionType}${error.name} from ${appName}`; if (!error.message) { console.log(`IBG-CRSH | Error message: ${error.message}`); error.message = errorMessage; } if (isPromise) { console.log('IBG-CRSH | Promise'); Promise.reject(error).then(() => Alert.alert(`Promise Rejection Crash report for ${error.name} is Sent!`), ); } else { try { throw ReferenceError(); } catch (e) { error.cause = e; throw error; } } } const [isEnabled, setIsEnabled] = useState(false); const [userAttributeKey, setUserAttributeKey] = useState(''); const [userAttributeValue, setUserAttributeValue] = useState(''); const [crashNameValue, setCrashNameValue] = useState(''); const [crashFingerprint, setCrashFingerprint] = useState(''); const [crashLevelValue, setCrashLevelValue] = useState( NonFatalErrorLevel.error, ); const toggleSwitch = (value: boolean) => { setIsEnabled(value); CrashReporting.setEnabled(value); showNotification('Crash Reporting status', 'Crash Reporting enabled set to ' + value); }; function sendCrash() { try { const error = new Error(crashNameValue); throw error; } catch (err) { if (err instanceof Error) { const attrMap: { [k: string]: string } = {}; attrMap[userAttributeKey] = userAttributeValue; const userAttributes: Record = {}; if (userAttributeKey && userAttributeValue) { userAttributes[userAttributeKey] = userAttributeValue; } const fingerprint = crashFingerprint.length === 0 ? undefined : crashFingerprint; CrashReporting.reportError(err, { userAttributes: userAttributes, fingerprint: fingerprint, level: crashLevelValue, }).then(() => { Alert.alert(`Crash report for ${crashNameValue} is Sent!`); }); } } } return ( Crash Reporting Enabled:
throwHandledException(new Error())} /> throwHandledException(new SyntaxError())} /> throwHandledException(new RangeError())} /> throwHandledException(new ReferenceError())} /> throwHandledException(new URIError())} /> NativeExampleCrashReporting.sendNativeNonFatal()} /> setCrashNameValue(key)} value={crashNameValue} /> setUserAttributeKey(key)} value={userAttributeKey} /> setUserAttributeValue(value)} value={userAttributeValue} />