11
22package com .masteratul .exceptionhandler ;
3+
34import android .app .Activity ;
45import android .content .Intent ;
56import android .util .Log ;
67
8+ import com .facebook .react .bridge .Callback ;
79import com .facebook .react .bridge .ReactApplicationContext ;
810import com .facebook .react .bridge .ReactContextBaseJavaModule ;
911import com .facebook .react .bridge .ReactMethod ;
10- import com .facebook .react .bridge .Callback ;
1112
1213public class ReactNativeExceptionHandlerModule extends ReactContextBaseJavaModule {
1314
14- private ReactApplicationContext reactContext ;
15+ private ReactApplicationContext reactContext ;
1516 private Activity activity ;
1617 private static Class errorIntentTargetClass = DefaultErrorScreen .class ;
18+ private static NativeExceptionHandlerIfc nativeExceptionHandler ;
1719 private Callback callbackHolder ;
1820 private Thread .UncaughtExceptionHandler originalHandler ;
1921
2022 public ReactNativeExceptionHandlerModule (ReactApplicationContext reactContext ) {
21- super (reactContext );
22- this .reactContext = reactContext ;
23- }
24-
25- @ Override
26- public String getName () {
27- return "ReactNativeExceptionHandler" ;
28- }
29-
30-
31- @ ReactMethod
32- public void setHandlerforNativeException (final boolean forceToQuit , Callback customHandler ){
33- callbackHolder = customHandler ;
34- originalHandler = Thread .getDefaultUncaughtExceptionHandler ();
35-
36- Thread .setDefaultUncaughtExceptionHandler (new Thread .UncaughtExceptionHandler () {
37- @ Override
38- public void uncaughtException (Thread thread , Throwable throwable ) {
39- activity = getCurrentActivity ();
40- String stackTraceString = Log .getStackTraceString (throwable );
41- callbackHolder .invoke (stackTraceString );
42- Log .d ("ERROR" ,stackTraceString );
43-
44-
45- Intent i = new Intent ();
46- i .setClass (activity , errorIntentTargetClass );
47- i .putExtra ("stack_trace_string" ,stackTraceString );
48- i .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
49-
50- activity .startActivity (i );
51- activity .finish ();
52-
53- if (originalHandler != null ) {
54- originalHandler .uncaughtException (thread , throwable );
55- }
56-
57- if (forceToQuit ) {
58- System .exit (0 );
59- }
60- }
61- });
62- }
63-
64- public static void replaceErrorScreenActivityClass (Class errorScreenActivityClass ){
65- errorIntentTargetClass = errorScreenActivityClass ;
66- }
67- }
23+ super (reactContext );
24+ this .reactContext = reactContext ;
25+ }
26+
27+ @ Override
28+ public String getName () {
29+ return "ReactNativeExceptionHandler" ;
30+ }
31+
32+
33+ @ ReactMethod
34+ public void setHandlerforNativeException (
35+ final boolean executeOriginalUncaughtExceptionHandler ,
36+ final boolean forceToQuit ,
37+ Callback customHandler ) {
38+
39+ callbackHolder = customHandler ;
40+ originalHandler = Thread .getDefaultUncaughtExceptionHandler ();
41+
42+ Thread .setDefaultUncaughtExceptionHandler (new Thread .UncaughtExceptionHandler () {
43+
44+ @ Override
45+ public void uncaughtException (Thread thread , Throwable throwable ) {
46+
47+ String stackTraceString = Log .getStackTraceString (throwable );
48+ callbackHolder .invoke (stackTraceString );
49+
50+ if (executeOriginalUncaughtExceptionHandler && originalHandler != null ) {
51+ originalHandler .uncaughtException (thread , throwable );
52+ }
53+
54+ if (nativeExceptionHandler != null ) {
55+ nativeExceptionHandler .handleNativeException (thread , throwable );
56+ } else {
57+ activity = getCurrentActivity ();
58+
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 );
63+
64+ activity .startActivity (i );
65+ activity .finish ();
66+
67+ if (forceToQuit ) {
68+ System .exit (0 );
69+ }
70+
71+ }
72+ }
73+ });
74+ }
75+
76+ public static void replaceErrorScreenActivityClass (Class errorScreenActivityClass ){
77+ errorIntentTargetClass = errorScreenActivityClass ;
78+ }
79+
80+ public static void setNativeExceptionHandler (NativeExceptionHandlerIfc nativeExceptionHandler ) {
81+ ReactNativeExceptionHandlerModule .nativeExceptionHandler = nativeExceptionHandler ;
82+ }
83+ }
0 commit comments