Skip to content

Commit ec40bac

Browse files
committed
Added examples
1 parent 50770af commit ec40bac

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

.eslintrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
},
1616
"extends": ["eslint:recommended", "plugin:react/recommended"],
1717
"rules": {
18-
"react-native/split-platform-components": 2,
1918
"react/no-did-mount-set-state": 2,
2019
"react/no-did-update-set-state": 2,
2120
"react/no-direct-mutation-state": 2,

examples/bugCaptureOnError.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {Alert} from 'react-native';
2+
import {BackAndroid} from 'react-native';
3+
import {setJSExceptionHandler} from 'react-native-exception-handler';
4+
5+
const reporter = (error) => {
6+
// Logic for reporting to devs
7+
// Example : Log issues to github issues using github apis.
8+
console.log(error); // sample
9+
};
10+
11+
const errorHandler = (e, isFatal) => {
12+
if (isFatal) {
13+
reporter(e);
14+
Alert.alert(
15+
'Unexpected error occurred',
16+
`
17+
Error: ${(isFatal) ? 'Fatal:' : ''} ${e.name} ${e.message}
18+
19+
We have reported this to our team ! Please close the app and start again!
20+
`,
21+
[{
22+
text: 'Close',
23+
onPress: () => {
24+
BackAndroid.exitApp();
25+
}
26+
}]
27+
);
28+
} else {
29+
console.log(e); // So that we can see it in the ADB logs in case of Android if needed
30+
}
31+
};
32+
33+
setJSExceptionHandler(errorHandler);

errorHandler.js renamed to examples/restartOnError.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {Alert} from 'react-native';
2+
import RNRestart from 'react-native-restart';
3+
import {setJSExceptionHandler} from 'react-native-exception-handler';
24

35
const errorHandler = (e, isFatal) => {
46
if (isFatal) {
@@ -12,7 +14,7 @@ const errorHandler = (e, isFatal) => {
1214
[{
1315
text: 'Restart',
1416
onPress: () => {
15-
console.log('test');
17+
RNRestart.Restart();
1618
}
1719
}]
1820
);
@@ -21,4 +23,4 @@ const errorHandler = (e, isFatal) => {
2123
}
2224
};
2325

24-
export default errorHandler;
26+
setJSExceptionHandler(errorHandler);

index.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
import errorHandler from './errorHandler';
1+
const noop = () => {};
22

3-
export const handleExceptions = (allowedInDevMode = false) => {
4-
registerErrorHandler(errorHandler, allowedInDevMode);
5-
};
6-
7-
export const registerErrorHandler = (customHandler, allowedInDevMode = false) => {
3+
export const setJSExceptionHandler = (customHandler = noop, allowedInDevMode = false) => {
84
const allowed = allowedInDevMode ? true : !__DEV__;
95
if (allowed) {
10-
if (customHandler) {
11-
global.ErrorUtils.setGlobalHandler(customHandler);
12-
} else {
13-
console.log('Custom Error Handler not passed to registerErrorHandler');
14-
}
6+
global.ErrorUtils.setGlobalHandler(customHandler);
157
} else {
16-
console.log('Not registering the error handler since its in dev mode and allowedInDevMode is not true');
8+
console.log('Skipping setJSExceptionHandler: Reason: In DEV mode and allowedInDevMode = false');
179
}
1810
};
11+
12+
export default {
13+
setJSExceptionHandler
14+
};

0 commit comments

Comments
 (0)