Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# react-native-exception-handler

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

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

setJSExceptionHandler(errorHandler, true); //Second argument true is basically
//if u need the handler to be called in place of RED
//if u need the handler to be called in place of RED
//screen in development mode also
```
or

setJSExceptionHandler(errorHandler, true, true); //Third argument allows adding it
//as a new handler, but keeping the previous one
//(it will run errorHandler but still show the red screen)
```


### Screens
Expand Down
1 change: 0 additions & 1 deletion examples/bugCaptureOnError.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Alert} from 'react-native';
import {BackAndroid} from 'react-native';
import {setJSExceptionHandler} from 'react-native-exception-handler';

const reporter = (error) => {
Expand Down
18 changes: 18 additions & 0 deletions examples/seamlessCapture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {setJSExceptionHandler} from 'react-native-exception-handler';

const reporter = (error) => {
// Logic for reporting to devs
// Example : Log issues to github issues using github apis.
console.log(error); // sample
};

const errorHandler = (e, isFatal) => {
if (isFatal) {
reporter(e);
} else {
console.log(e); // So that we can see it in the ADB logs in case of Android if needed
}
};

// We will still see the error screen, but our reporter() function will be called
setJSExceptionHandler(errorHandler, false, true);
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const noop = () => {};

export const setJSExceptionHandler = (customHandler = noop, allowedInDevMode = false) => {
export const setJSExceptionHandler = (customHandler = noop, allowedInDevMode = false, keepPreviousHandler = false) => {
const allowed = allowedInDevMode ? true : !__DEV__;
if (allowed) {
global.ErrorUtils.setGlobalHandler(customHandler);
if (keepPreviousHandler) {
const previousHandler = global.ErrorUtils.getGlobalHandler();
global.ErrorUtils.setGlobalHandler((error, isFatal) => {
customHandler(error, isFatal);
previousHandler(error, isFatal);
});
} else {
global.ErrorUtils.setGlobalHandler(customHandler);
}
} else {
console.log('Skipping setJSExceptionHandler: Reason: In DEV mode and allowedInDevMode = false');
}
Expand Down