File tree Expand file tree Collapse file tree 4 files changed +36
-6
lines changed Expand file tree Collapse file tree 4 files changed +36
-6
lines changed Original file line number Diff line number Diff line change 11# react-native-exception-handler
22
33A 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
66In 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
4646or
4747
4848setJSExceptionHandler (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
Original file line number Diff line number Diff line change 11import { Alert } from 'react-native' ;
2- import { BackAndroid } from 'react-native' ;
32import { setJSExceptionHandler } from 'react-native-exception-handler' ;
43
54const reporter = ( error ) => {
Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff line change 11const 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 }
You can’t perform that action at this time.
0 commit comments