From 3a3a44bc56a139acf68d9146a0fb3d8299d4ea4f Mon Sep 17 00:00:00 2001 From: zeh fernando Date: Thu, 15 Jun 2017 11:20:45 -0400 Subject: [PATCH 1/2] Added ability to keep previous exception handler This adds a new boolean parameter, `keepPreviousHandler`. When set to true, it stores the previous handler value and calls it after calling the newly set exception handler. This allows developers to keep the previous functionality (red screen) intact, while still capturing errors (i.e. for analytics purposes). --- index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 0662c47..e8623c0 100644 --- a/index.js +++ b/index.js @@ -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'); } From be9aa6621b7b10ae884963a8b4c14895d91e6683 Mon Sep 17 00:00:00 2001 From: Zeh Fernando Date: Thu, 15 Jun 2017 11:29:53 -0400 Subject: [PATCH 2/2] Added documentation for the third parameter, `keepPreviousHandler` --- README.md | 11 ++++++++--- examples/bugCaptureOnError.js | 1 - examples/seamlessCapture.js | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 examples/seamlessCapture.js diff --git a/README.md b/README.md index 2bf2ebb..44954eb 100644 --- a/README.md +++ b/README.md @@ -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.` @@ -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 diff --git a/examples/bugCaptureOnError.js b/examples/bugCaptureOnError.js index 936eb66..9b5e53e 100644 --- a/examples/bugCaptureOnError.js +++ b/examples/bugCaptureOnError.js @@ -1,5 +1,4 @@ import {Alert} from 'react-native'; -import {BackAndroid} from 'react-native'; import {setJSExceptionHandler} from 'react-native-exception-handler'; const reporter = (error) => { diff --git a/examples/seamlessCapture.js b/examples/seamlessCapture.js new file mode 100644 index 0000000..684bfe7 --- /dev/null +++ b/examples/seamlessCapture.js @@ -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);