|
1 | 1 | #import "Bugsnag.h" |
2 | 2 | #import "BSG_KSCrashC.h" |
3 | 3 | #import "BugsnagReactNative.h" |
| 4 | +#import "RCTVersion.h" |
4 | 5 | #import <React/RCTConvert.h> |
5 | 6 |
|
6 | 7 | NSString *const BSGInfoPlistKey = @"BugsnagAPIKey"; |
@@ -271,6 +272,23 @@ + (void)startWithConfiguration:(BugsnagConfiguration *)config { |
271 | 272 | config.shouldAutoCaptureSessions = [RCTConvert BOOL:options[@"autoCaptureSessions"]]; |
272 | 273 | config.automaticallyCollectBreadcrumbs = [RCTConvert BOOL:options[@"automaticallyCollectBreadcrumbs"]]; |
273 | 274 |
|
| 275 | + [config addBeforeSendSession:^void(NSMutableDictionary *_Nonnull data) { |
| 276 | + data[@"device"] = [self addDeviceRuntimeVersion:data[@"device"] |
| 277 | + reactNativeVersion:[self findReactNativeVersion]]; |
| 278 | + }]; |
| 279 | + |
| 280 | + [config addBeforeSendBlock:^bool(NSDictionary *_Nonnull rawEventData, |
| 281 | + BugsnagCrashReport *_Nonnull report) { |
| 282 | + NSString *reactNativeVersion = report.metaData[@"_bugsnag"][@"reactNativeVersion"]; |
| 283 | + if (reactNativeVersion != nil) { |
| 284 | + report.device = [self addDeviceRuntimeVersion:report.device reactNativeVersion:reactNativeVersion]; |
| 285 | + } |
| 286 | + report.metaData = [self removeRuntimeVersionFromMetaData:report]; |
| 287 | + |
| 288 | + return !([report.errorClass hasPrefix:@"RCTFatalException"] |
| 289 | + && [report.errorMessage hasPrefix:@"Unhandled JS Exception"]); |
| 290 | + }]; |
| 291 | + |
274 | 292 | if (notifyURLPath.length > 0) { |
275 | 293 | [config setEndpointsForNotify:notifyURLPath |
276 | 294 | sessions:sessionURLPath]; |
@@ -299,6 +317,68 @@ + (void)startWithConfiguration:(BugsnagConfiguration *)config { |
299 | 317 | // session to compensate. |
300 | 318 | [Bugsnag resumeSession]; |
301 | 319 | } |
| 320 | + [self addRuntimeVersionToMetaData:config]; |
| 321 | +} |
| 322 | + |
| 323 | +/** |
| 324 | + * Stores runtime version info in a metadata tab (it will be moved to a |
| 325 | + * different payload location before sending) |
| 326 | + */ |
| 327 | +- (void)addRuntimeVersionToMetaData:(BugsnagConfiguration *)config { |
| 328 | + [config.metaData addAttribute:@"reactNativeVersion" |
| 329 | + withValue:[self findReactNativeVersion] |
| 330 | + toTabWithName:@"_bugsnag"]; |
| 331 | +} |
| 332 | + |
| 333 | +- (NSDictionary *)removeRuntimeVersionFromMetaData:(BugsnagCrashReport *)report { |
| 334 | + NSMutableDictionary *metadata = report.metaData.mutableCopy; |
| 335 | + metadata[@"_bugsnag"] = nil; |
| 336 | + return metadata; |
| 337 | +} |
| 338 | + |
| 339 | +- (NSDictionary *)addDeviceRuntimeVersion:(NSDictionary *)device |
| 340 | + reactNativeVersion:(NSString *)reactNativeVersion { |
| 341 | + NSMutableDictionary *copy = [device mutableCopy]; |
| 342 | + NSMutableDictionary *runtimeVersions = [copy[@"runtimeVersions"] mutableCopy]; |
| 343 | + |
| 344 | + if (runtimeVersions == nil) { |
| 345 | + runtimeVersions = [NSMutableDictionary new]; |
| 346 | + } |
| 347 | + runtimeVersions[@"reactNative"] = reactNativeVersion; |
| 348 | + copy[@"runtimeVersions"] = runtimeVersions; |
| 349 | + return copy; |
| 350 | +} |
| 351 | + |
| 352 | +// see https://github.com/facebook/react-native/blob/6df2edeb2a33d529e4b13a5b6767f300d08aeb0a/scripts/bump-oss-version.js |
| 353 | +- (NSString *)findReactNativeVersion { |
| 354 | + static dispatch_once_t onceToken; |
| 355 | + static NSString *BSGReactNativeVersion = nil; |
| 356 | + dispatch_once(&onceToken, ^{ |
| 357 | + NSDictionary *versionMap = RCTGetReactNativeVersion(); |
| 358 | + NSNumber *major = versionMap[@"major"]; |
| 359 | + NSNumber *minor = versionMap[@"minor"]; |
| 360 | + NSNumber *patch = versionMap[@"patch"]; |
| 361 | + NSString *prerelease = versionMap[@"prerelease"]; |
| 362 | + NSMutableString *versionString = [NSMutableString new]; |
| 363 | + |
| 364 | + if (![major isEqual:[NSNull null]]) { |
| 365 | + [versionString appendString:[major stringValue]]; |
| 366 | + [versionString appendString:@"."]; |
| 367 | + } |
| 368 | + if (![minor isEqual:[NSNull null]]) { |
| 369 | + [versionString appendString:[minor stringValue]]; |
| 370 | + [versionString appendString:@"."]; |
| 371 | + } |
| 372 | + if (![patch isEqual:[NSNull null]]) { |
| 373 | + [versionString appendString:[patch stringValue]]; |
| 374 | + } |
| 375 | + if (![prerelease isEqual:[NSNull null]]) { |
| 376 | + [versionString appendString:@"-"]; |
| 377 | + [versionString appendString:prerelease]; |
| 378 | + } |
| 379 | + BSGReactNativeVersion = [NSString stringWithString:versionString]; |
| 380 | + }); |
| 381 | + return BSGReactNativeVersion; |
302 | 382 | } |
303 | 383 |
|
304 | 384 | - (void)setNotifierDetails:(NSString *)packageVersion { |
|
0 commit comments