2
2
3
3
4
4
// CONSTANTS
5
- NSString * const UncaughtExceptionHandlerSignalExceptionName = @" UncaughtExceptionHandlerSignalExceptionName " ;
6
- NSString * const UncaughtExceptionHandlerSignalKey = @" UncaughtExceptionHandlerSignalKey " ;
7
- NSString * const UncaughtExceptionHandlerAddressesKey = @" UncaughtExceptionHandlerAddressesKey " ;
8
- volatile int32_t UncaughtExceptionCount = 0 ;
9
- const int32_t UncaughtExceptionMaximum = 10 ;
10
- const NSInteger UncaughtExceptionHandlerSkipAddressCount = 4 ;
11
- const NSInteger UncaughtExceptionHandlerReportAddressCount = 5 ;
5
+ NSString * const RNUncaughtExceptionHandlerSignalExceptionName = @" RNUncaughtExceptionHandlerSignalExceptionName " ;
6
+ NSString * const RNUncaughtExceptionHandlerSignalKey = @" RNUncaughtExceptionHandlerSignalKey " ;
7
+ NSString * const RNUncaughtExceptionHandlerAddressesKey = @" RNUncaughtExceptionHandlerAddressesKey " ;
8
+ volatile int32_t RNUncaughtExceptionCount = 0 ;
9
+ const int32_t RNUncaughtExceptionMaximum = 10 ;
10
+ const NSInteger RNUncaughtExceptionHandlerSkipAddressCount = 4 ;
11
+ const NSInteger RNUncaughtExceptionHandlerReportAddressCount = 5 ;
12
12
13
13
14
14
@implementation ReactNativeExceptionHandler
@@ -34,18 +34,18 @@ - (dispatch_queue_t)methodQueue
34
34
// variable that holds the default native error handler
35
35
void (^defaultNativeErrorCallbackBlock)(NSException *exception, NSString *readeableException) =
36
36
^(NSException *exception, NSString *readeableException){
37
-
37
+
38
38
UIAlertController* alert = [UIAlertController
39
39
alertControllerWithTitle: @" Unexpected error occured"
40
40
message: [NSString stringWithFormat: @" %@ \n %@ " ,
41
41
@" Appologies..The app will close now \n Please restart the app\n " ,
42
42
readeableException]
43
43
preferredStyle: UIAlertControllerStyleAlert];
44
-
44
+
45
45
UIApplication* app = [UIApplication sharedApplication ];
46
46
UIViewController * rootViewController = app.delegate .window .rootViewController ;
47
47
[rootViewController presentViewController: alert animated: YES completion: nil ];
48
-
48
+
49
49
[NSTimer scheduledTimerWithTimeInterval: 5.0
50
50
target: [ReactNativeExceptionHandler class ]
51
51
selector: @selector (releaseExceptionHold )
@@ -66,7 +66,7 @@ - (dispatch_queue_t)methodQueue
66
66
jsErrorCallbackBlock = ^(NSException *exception, NSString *readeableException){
67
67
callback (@[readeableException]);
68
68
};
69
-
69
+
70
70
NSSetUncaughtExceptionHandler (&HandleException);
71
71
signal (SIGABRT, SignalHandler);
72
72
signal (SIGILL, SignalHandler);
@@ -102,16 +102,16 @@ - (void)handleException:(NSException *)exception
102
102
{
103
103
NSString * readeableError = [NSString stringWithFormat: NSLocalizedString(@" %@ \n %@ " , nil ),
104
104
[exception reason ],
105
- [[exception userInfo ] objectForKey: UncaughtExceptionHandlerAddressesKey ]];
105
+ [[exception userInfo ] objectForKey: RNUncaughtExceptionHandlerAddressesKey ]];
106
106
dismissApp = false ;
107
-
107
+
108
108
if (nativeErrorCallbackBlock != nil ){
109
109
nativeErrorCallbackBlock (exception,readeableError);
110
110
}else {
111
111
defaultNativeErrorCallbackBlock (exception,readeableError);
112
112
}
113
113
jsErrorCallbackBlock (exception,readeableError);
114
-
114
+
115
115
CFRunLoopRef runLoop = CFRunLoopGetCurrent ();
116
116
CFArrayRef allModes = CFRunLoopCopyAllModes (runLoop);
117
117
while (!dismissApp)
@@ -126,18 +126,18 @@ - (void)handleException:(NSException *)exception
126
126
i++;
127
127
}
128
128
}
129
-
129
+
130
130
CFRelease (allModes);
131
-
131
+
132
132
NSSetUncaughtExceptionHandler (NULL );
133
133
signal (SIGABRT, SIG_DFL);
134
134
signal (SIGILL, SIG_DFL);
135
135
signal (SIGSEGV, SIG_DFL);
136
136
signal (SIGFPE, SIG_DFL);
137
137
signal (SIGBUS, SIG_DFL);
138
138
signal (SIGPIPE, SIG_DFL);
139
-
140
- kill (getpid (), [[[exception userInfo ] objectForKey: UncaughtExceptionHandlerSignalKey ] intValue ]);
139
+
140
+ kill (getpid (), [[[exception userInfo ] objectForKey: RNUncaughtExceptionHandlerSignalKey ] intValue ]);
141
141
142
142
}
143
143
@@ -148,19 +148,19 @@ - (void)handleException:(NSException *)exception
148
148
149
149
void HandleException (NSException *exception)
150
150
{
151
- int32_t exceptionCount = OSAtomicIncrement32 (&UncaughtExceptionCount );
152
- if (exceptionCount > UncaughtExceptionMaximum )
151
+ int32_t exceptionCount = OSAtomicIncrement32 (&RNUncaughtExceptionCount );
152
+ if (exceptionCount > RNUncaughtExceptionMaximum )
153
153
{
154
154
return ;
155
155
}
156
-
156
+
157
157
NSArray *callStack = [ReactNativeExceptionHandler backtrace ];
158
158
NSMutableDictionary *userInfo =
159
159
[NSMutableDictionary dictionaryWithDictionary: [exception userInfo ]];
160
160
[userInfo
161
161
setObject: callStack
162
- forKey: UncaughtExceptionHandlerAddressesKey ];
163
-
162
+ forKey: RNUncaughtExceptionHandlerAddressesKey ];
163
+
164
164
[[[ReactNativeExceptionHandler alloc ] init ]
165
165
performSelectorOnMainThread: @selector (handleException: )
166
166
withObject:
@@ -173,35 +173,35 @@ void HandleException(NSException *exception)
173
173
174
174
void SignalHandler (int signal)
175
175
{
176
- int32_t exceptionCount = OSAtomicIncrement32 (&UncaughtExceptionCount );
177
- if (exceptionCount > UncaughtExceptionMaximum )
176
+ int32_t exceptionCount = OSAtomicIncrement32 (&RNUncaughtExceptionCount );
177
+ if (exceptionCount > RNUncaughtExceptionMaximum )
178
178
{
179
179
return ;
180
180
}
181
-
181
+
182
182
NSMutableDictionary *userInfo =
183
183
[NSMutableDictionary
184
184
dictionaryWithObject: [NSNumber numberWithInt: signal]
185
- forKey: UncaughtExceptionHandlerSignalKey ];
186
-
185
+ forKey: RNUncaughtExceptionHandlerSignalKey ];
186
+
187
187
NSArray *callStack = [ReactNativeExceptionHandler backtrace ];
188
188
[userInfo
189
189
setObject: callStack
190
- forKey: UncaughtExceptionHandlerAddressesKey ];
191
-
190
+ forKey: RNUncaughtExceptionHandlerAddressesKey ];
191
+
192
192
[[[ReactNativeExceptionHandler alloc ] init ]
193
193
performSelectorOnMainThread: @selector (handleException: )
194
194
withObject:
195
195
[NSException
196
- exceptionWithName: UncaughtExceptionHandlerSignalExceptionName
196
+ exceptionWithName: RNUncaughtExceptionHandlerSignalExceptionName
197
197
reason:
198
198
[NSString stringWithFormat:
199
199
NSLocalizedString (@" Signal %d was raised." , nil ),
200
200
signal]
201
201
userInfo:
202
202
[NSDictionary
203
203
dictionaryWithObject:[NSNumber numberWithInt:signal]
204
- forKey:UncaughtExceptionHandlerSignalKey ]]
204
+ forKey:RNUncaughtExceptionHandlerSignalKey ]]
205
205
waitUntilDone:YES];
206
206
}
207
207
@@ -215,19 +215,19 @@ + (NSArray *)backtrace
215
215
void * callstack[128 ];
216
216
int frames = backtrace (callstack, 128 );
217
217
char **strs = backtrace_symbols (callstack, frames);
218
-
218
+
219
219
int i;
220
220
NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity: frames];
221
221
for (
222
- i = UncaughtExceptionHandlerSkipAddressCount ;
223
- i < UncaughtExceptionHandlerSkipAddressCount +
224
- UncaughtExceptionHandlerReportAddressCount ;
222
+ i = RNUncaughtExceptionHandlerSkipAddressCount ;
223
+ i < RNUncaughtExceptionHandlerSkipAddressCount +
224
+ RNUncaughtExceptionHandlerReportAddressCount ;
225
225
i++)
226
226
{
227
227
[backtrace addObject: [NSString stringWithUTF8String: strs[i]]];
228
228
}
229
229
free (strs);
230
-
230
+
231
231
return backtrace;
232
232
}
233
233
0 commit comments