Skip to content

Commit e58268e

Browse files
authored
Merge pull request #2 from zeh/keep-previous-handler
Keep previous handler
2 parents 9247a8b + be9aa66 commit e58268e

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# react-native-exception-handler
22

33
A react native module that lets you to register a global error handler that can capture fatal/non fatal uncaught exceptions.
4-
The module helps prevent abrupt crashing of RN Apps without a graceful message to the user.
4+
The module helps prevent abrupt crashing of RN Apps without a graceful message to the user.
55

66
In the current scenario:
77
- `In DEV mode , you get a RED Screen error pointing your JS errors.`
@@ -46,9 +46,14 @@ setJSExceptionHandler(errorHandler); // registering the error handler (maybe u c
4646
or
4747

4848
setJSExceptionHandler(errorHandler, true); //Second argument true is basically
49-
//if u need the handler to be called in place of RED
49+
//if u need the handler to be called in place of RED
5050
//screen in development mode also
51-
```
51+
or
52+
53+
setJSExceptionHandler(errorHandler, true, true); //Third argument allows adding it
54+
//as a new handler, but keeping the previous one
55+
//(it will run errorHandler but still show the red screen)
56+
```
5257

5358

5459
### Screens

examples/bugCaptureOnError.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {Alert} from 'react-native';
2-
import {BackAndroid} from 'react-native';
32
import {setJSExceptionHandler} from 'react-native-exception-handler';
43

54
const reporter = (error) => {

examples/seamlessCapture.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {setJSExceptionHandler} from 'react-native-exception-handler';
2+
3+
const reporter = (error) => {
4+
// Logic for reporting to devs
5+
// Example : Log issues to github issues using github apis.
6+
console.log(error); // sample
7+
};
8+
9+
const errorHandler = (e, isFatal) => {
10+
if (isFatal) {
11+
reporter(e);
12+
} else {
13+
console.log(e); // So that we can see it in the ADB logs in case of Android if needed
14+
}
15+
};
16+
17+
// We will still see the error screen, but our reporter() function will be called
18+
setJSExceptionHandler(errorHandler, false, true);

index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
const noop = () => {};
22

3-
export const setJSExceptionHandler = (customHandler = noop, allowedInDevMode = false) => {
3+
export const setJSExceptionHandler = (customHandler = noop, allowedInDevMode = false, keepPreviousHandler = false) => {
44
const allowed = allowedInDevMode ? true : !__DEV__;
55
if (allowed) {
6-
global.ErrorUtils.setGlobalHandler(customHandler);
6+
if (keepPreviousHandler) {
7+
const previousHandler = global.ErrorUtils.getGlobalHandler();
8+
global.ErrorUtils.setGlobalHandler((error, isFatal) => {
9+
customHandler(error, isFatal);
10+
previousHandler(error, isFatal);
11+
});
12+
} else {
13+
global.ErrorUtils.setGlobalHandler(customHandler);
14+
}
715
} else {
816
console.log('Skipping setJSExceptionHandler: Reason: In DEV mode and allowedInDevMode = false');
917
}

0 commit comments

Comments
 (0)