Skip to content

Commit babc046

Browse files
committed
This adds stacktrace merging for react-native 0.45
1 parent 56576a5 commit babc046

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

ios/RNSentry.m

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,23 +202,54 @@ - (void)setReleaseVersionDist:(SentryEvent *)event {
202202
if (NSClassFromString(@"RCTBatchedBridge")) {
203203
[self swizzleCallNativeModule:NSClassFromString(@"RCTBatchedBridge")];
204204
} else {
205-
// TODO fix stacktrace merging
206-
reject(@"SentryException", @"stacktrace merging not supported", nil);
207-
return;
205+
[self swizzleInvokeWithBridge:NSClassFromString(@"RCTModuleMethod")];
208206
}
209207
resolve(@YES);
210208
}
211209

212-
- (void)swizzleCallNativeModule:(Class)class {
210+
- (void)swizzleInvokeWithBridge:(Class)class {
213211
static const void *key = &key;
214-
SEL selector = @selector(callNativeModule:method:params:);
212+
SEL selector = @selector(invokeWithBridge:module:arguments:);
215213
uintptr_t callNativeModuleAddress = [class instanceMethodForSelector:selector];
216214

217215
SentrySwizzleInstanceMethod(class,
218216
selector,
219217
SentrySWReturnType(id),
218+
SentrySWArguments(RCTBridge *bridge, id module, NSArray *arguments),
219+
SentrySWReplacement({
220+
// TODO: refactor this block, its used twice
221+
NSMutableArray *newParams = [NSMutableArray array];
222+
if (arguments != nil && arguments.count > 0) {
223+
for (id param in arguments) {
224+
if ([param isKindOfClass:NSDictionary.class] && param[@"__sentry_stack"]) {
225+
@synchronized (SentryClient.sharedClient) {
226+
NSMutableDictionary *prevExtra = SentryClient.sharedClient.extra.mutableCopy;
227+
[prevExtra setValue:[NSNumber numberWithUnsignedInteger:callNativeModuleAddress] forKey:@"__sentry_address"];
228+
[prevExtra setValue:SentryParseJavaScriptStacktrace([RCTConvert NSString:param[@"__sentry_stack"]]) forKey:@"__sentry_stack"];
229+
SentryClient.sharedClient.extra = prevExtra;
230+
}
231+
} else {
232+
if (param != nil) {
233+
[newParams addObject:param];
234+
}
235+
}
236+
}
237+
}
238+
return SentrySWCallOriginal(bridge, module, newParams);
239+
}), SentrySwizzleModeOncePerClassAndSuperclasses, key);
240+
}
241+
242+
- (void)swizzleCallNativeModule:(Class)class {
243+
static const void *key = &key;
244+
SEL selctor = @selector(callNativeModule:method:params:);
245+
uintptr_t callNativeModuleAddress = [class instanceMethodForSelector:selctor];
246+
247+
SentrySwizzleInstanceMethod(class,
248+
selctor,
249+
SentrySWReturnType(id),
220250
SentrySWArguments(NSUInteger moduleID, NSUInteger methodID, NSArray *params),
221251
SentrySWReplacement({
252+
// TODO: refactor this block, its used twice
222253
NSMutableArray *newParams = [NSMutableArray array];
223254
if (params != nil && params.count > 0) {
224255
for (id param in params) {

lib/Sentry.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,18 @@ class NativeClient {
256256
return;
257257
}
258258
this._ignoredModules = {};
259-
__fbBatchedBridgeConfig.remoteModuleConfig.forEach((module, moduleID) => {
260-
if (
261-
module !== null &&
262-
this.options.ignoreModulesExclude.indexOf(module[0]) === -1 &&
263-
(DEFAULT_MODULE_IGNORES.indexOf(module[0]) >= 0 ||
264-
this.options.ignoreModulesInclude.indexOf(module[0]) >= 0)
265-
) {
266-
this._ignoredModules[moduleID] = true;
267-
}
268-
});
259+
if (typeof __fbBatchedBridgeConfig !== 'undefined') {
260+
__fbBatchedBridgeConfig.remoteModuleConfig.forEach((module, moduleID) => {
261+
if (
262+
module !== null &&
263+
this.options.ignoreModulesExclude.indexOf(module[0]) === -1 &&
264+
(DEFAULT_MODULE_IGNORES.indexOf(module[0]) >= 0 ||
265+
this.options.ignoreModulesInclude.indexOf(module[0]) >= 0)
266+
) {
267+
this._ignoredModules[moduleID] = true;
268+
}
269+
});
270+
}
269271
this._activatedMerging = true;
270272
this._overwriteEnqueueNativeCall();
271273
})

0 commit comments

Comments
 (0)