1212
1313public class ReactNativeExceptionHandlerModule extends ReactContextBaseJavaModule {
1414
15- private ReactApplicationContext reactContext ;
15+ private ReactApplicationContext reactContext ;
1616 private Activity activity ;
1717 private static Class errorIntentTargetClass = DefaultErrorScreen .class ;
1818 private static NativeExceptionHandlerIfc nativeExceptionHandler ;
1919 private Callback callbackHolder ;
2020 private Thread .UncaughtExceptionHandler originalHandler ;
2121
2222 public ReactNativeExceptionHandlerModule (ReactApplicationContext reactContext ) {
23- super (reactContext );
24- this .reactContext = reactContext ;
25- }
23+ super (reactContext );
24+ this .reactContext = reactContext ;
25+ }
2626
27- @ Override
28- public String getName () {
29- return "ReactNativeExceptionHandler" ;
30- }
27+ @ Override
28+ public String getName () {
29+ return "ReactNativeExceptionHandler" ;
30+ }
3131
3232
33- @ ReactMethod
34- public void setHandlerforNativeException (
35- final boolean executeOriginalUncaughtExceptionHandler ,
36- final boolean forceToQuit ,
37- Callback customHandler ) {
33+ @ ReactMethod
34+ public void setHandlerforNativeException (
35+ final boolean executeOriginalUncaughtExceptionHandler ,
36+ final boolean forceToQuit ,
37+ Callback customHandler ) {
3838
39- callbackHolder = customHandler ;
40- originalHandler = Thread .getDefaultUncaughtExceptionHandler ();
39+ callbackHolder = customHandler ;
40+ originalHandler = Thread .getDefaultUncaughtExceptionHandler ();
4141
42- Thread .setDefaultUncaughtExceptionHandler (new Thread .UncaughtExceptionHandler () {
42+ Thread .setDefaultUncaughtExceptionHandler (new Thread .UncaughtExceptionHandler () {
4343
44- @ Override
45- public void uncaughtException (Thread thread , Throwable throwable ) {
44+ @ Override
45+ public void uncaughtException (Thread thread , Throwable throwable ) {
4646
47- String stackTraceString = Log .getStackTraceString (throwable );
48- callbackHolder .invoke (stackTraceString );
47+ String stackTraceString = Log .getStackTraceString (throwable );
48+ callbackHolder .invoke (stackTraceString );
4949
50- if (executeOriginalUncaughtExceptionHandler && originalHandler != null ) {
51- originalHandler .uncaughtException (thread , throwable );
52- }
50+ if (nativeExceptionHandler != null ) {
51+ nativeExceptionHandler .handleNativeException (thread , throwable , originalHandler );
52+ } else {
53+ activity = getCurrentActivity ();
5354
54- if ( nativeExceptionHandler != null ) {
55- nativeExceptionHandler . handleNativeException ( thread , throwable );
56- } else {
57- activity = getCurrentActivity ( );
55+ Intent i = new Intent ();
56+ i . setClass ( activity , errorIntentTargetClass );
57+ i . putExtra ( "stack_trace_string" , stackTraceString );
58+ i . setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK );
5859
59- Intent i = new Intent ();
60- i .setClass (activity , errorIntentTargetClass );
61- i .putExtra ("stack_trace_string" ,stackTraceString );
62- i .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
60+ activity .startActivity (i );
61+ activity .finish ();
6362
64- activity .startActivity (i );
65- activity .finish ();
63+ if (executeOriginalUncaughtExceptionHandler && originalHandler != null ) {
64+ originalHandler .uncaughtException (thread , throwable );
65+ }
6666
67- if (forceToQuit ) {
68- System .exit (0 );
69- }
67+ if (forceToQuit ) {
68+ System .exit (0 );
69+ }
70+ }
71+ }
72+ });
73+ }
7074
71- }
72- }
73- });
74- }
75-
76- public static void replaceErrorScreenActivityClass (Class errorScreenActivityClass ){
77- errorIntentTargetClass = errorScreenActivityClass ;
78- }
75+ public static void replaceErrorScreenActivityClass (Class errorScreenActivityClass ){
76+ errorIntentTargetClass = errorScreenActivityClass ;
77+ }
7978
8079 public static void setNativeExceptionHandler (NativeExceptionHandlerIfc nativeExceptionHandler ) {
8180 ReactNativeExceptionHandlerModule .nativeExceptionHandler = nativeExceptionHandler ;
8281 }
83- }
82+ }
0 commit comments