Skip to content

Commit 553e027

Browse files
noahsmartinantonis
andauthored
Support sentry SDK Swift (#5025)
* Support sentry SDK Swift * Fix latest version * Update packages/core/ios/RNSentry.h Co-authored-by: Antonis Lilis <[email protected]> * Use SentrySDKInternal for options * Fixes --------- Co-authored-by: Antonis Lilis <[email protected]>
1 parent 9f3972e commit 553e027

File tree

9 files changed

+94
-35
lines changed

9 files changed

+94
-35
lines changed

packages/core/RNSentryCocoaTester/RNSentryCocoaTesterTests/RNSentryDependencyContainerTests.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
#import <Foundation/Foundation.h>
33
#import <RNSentry/RNSentry.h>
44

5-
@interface
6-
SentrySDK (PrivateTests)
7-
- (nullable SentryOptions *)options;
8-
@end
9-
105
@interface SentryDependencyContainer : NSObject
116
+ (instancetype)sharedInstance;
127
@property (nonatomic, strong) SentryFramesTracker *framesTracker;

packages/core/RNSentryCocoaTester/RNSentryCocoaTesterTests/RNSentryFramesTrackerListenerTests.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
#import <Foundation/Foundation.h>
33
#import <RNSentry/RNSentry.h>
44

5-
@interface
6-
SentrySDK (PrivateTests)
7-
- (nullable SentryOptions *)options;
8-
@end
9-
105
@interface SentryDependencyContainer : NSObject
116
+ (instancetype)sharedInstance;
127
@property (nonatomic, strong) SentryFramesTracker *framesTracker;
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#import <Foundation/Foundation.h>
22
#import <RNSentry/RNSentry.h>
3-
@import Sentry;
43

4+
@class SentryOptions;
5+
6+
#if CROSS_PLATFORM_TEST
7+
@interface
8+
SentrySDKInternal (PrivateTests)
9+
#else
510
@interface
611
SentrySDK (PrivateTests)
7-
- (nullable SentryOptions *)options;
12+
#endif
13+
+ (nullable SentryOptions *)options;
814
@end

packages/core/RNSentryCocoaTester/RNSentryCocoaTesterTests/RNSentryTests.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#import <Sentry/SentryDebugImageProvider+HybridSDKs.h>
55
#import <UIKit/UIKit.h>
66
#import <XCTest/XCTest.h>
7+
@import Sentry;
78

89
@interface RNSentryInitNativeSdkTests : XCTestCase
910

@@ -464,8 +465,13 @@ - (void)prepareNativeFrameMocksWithLocalSymbolication:(BOOL)debug
464465
SentryOptions *sentryOptions = [[SentryOptions alloc] init];
465466
sentryOptions.debug = debug; // no local symbolication
466467

468+
#if CROSS_PLATFORM_TEST
469+
id sentrySDKMock = OCMClassMock([SentrySDKInternal class]);
470+
OCMStub([(Class)sentrySDKMock options]).andReturn(sentryOptions);
471+
#else
467472
id sentrySDKMock = OCMClassMock([SentrySDK class]);
468-
OCMStub([(SentrySDK *)sentrySDKMock options]).andReturn(sentryOptions);
473+
OCMStub([(Class)sentrySDKMock options]).andReturn(sentryOptions);
474+
#endif
469475

470476
id sentryDependencyContainerMock = OCMClassMock([SentryDependencyContainer class]);
471477
OCMStub(ClassMethod([sentryDependencyContainerMock sharedInstance]))

packages/core/ios/RNSentry.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99

1010
#import <Sentry/Sentry.h>
1111
#import <Sentry/SentryDebugImageProvider.h>
12-
#import <Sentry/SentryOptions.h>
1312

1413
typedef int (*SymbolicateCallbackType)(const void *, Dl_info *);
1514

15+
@class SentryOptions;
16+
@class SentryEvent;
17+
18+
#if CROSS_PLATFORM_TEST
19+
@interface SentrySDKInternal : NSObject
20+
#else
1621
@interface
1722
SentrySDK (Private)
23+
#endif
1824
@property (nonatomic, nullable, readonly, class) SentryOptions *options;
1925
@end
2026

packages/core/ios/RNSentry.mm

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@
2121
#import <Sentry/PrivateSentrySDKOnly.h>
2222
#import <Sentry/SentryAppStartMeasurement.h>
2323
#import <Sentry/SentryBinaryImageCache.h>
24+
#import <Sentry/SentryBreadcrumb.h>
2425
#import <Sentry/SentryDebugImageProvider+HybridSDKs.h>
26+
#import <Sentry/SentryDebugMeta.h>
2527
#import <Sentry/SentryDependencyContainer.h>
28+
#import <Sentry/SentryEvent.h>
29+
#import <Sentry/SentryException.h>
2630
#import <Sentry/SentryFormatter.h>
31+
#import <Sentry/SentryOptions.h>
32+
#import <Sentry/SentryUser.h>
2733
#if __has_include(<Sentry/SentryOptions+HybridSDKs.h>)
2834
# define USE_SENTRY_OPTIONS 1
2935
# import <Sentry/SentryOptions+HybridSDKs.h>
@@ -57,15 +63,7 @@
5763

5864
#import "RNSentryExperimentalOptions.h"
5965
#import "RNSentryVersion.h"
60-
61-
@interface
62-
SentrySDK (RNSentry)
63-
64-
+ (void)captureEnvelope:(SentryEnvelope *)envelope;
65-
66-
+ (void)storeEnvelope:(SentryEnvelope *)envelope;
67-
68-
@end
66+
#import "SentrySDKWrapper.h"
6967

7068
static bool hasFetchedAppStart;
7169

@@ -112,7 +110,7 @@ - (instancetype)init
112110
[PrivateSentrySDKOnly addSdkPackage:REACT_NATIVE_SDK_PACKAGE_NAME
113111
version:REACT_NATIVE_SDK_PACKAGE_VERSION];
114112

115-
[SentrySDK startWithOptions:sentryOptions];
113+
[SentrySDKWrapper startWithOptions:sentryOptions];
116114

117115
#if TARGET_OS_IPHONE || TARGET_OS_MACCATALYST
118116
BOOL appIsActive =
@@ -363,7 +361,11 @@ - (void)stopObserving
363361
- (NSDictionary *)fetchNativeStackFramesBy:(NSArray<NSNumber *> *)instructionsAddr
364362
symbolicate:(SymbolicateCallbackType)symbolicate
365363
{
364+
#if CROSS_PLATFORM_TEST
365+
BOOL shouldSymbolicateLocally = [SentrySDKInternal.options debug];
366+
#else
366367
BOOL shouldSymbolicateLocally = [SentrySDK.options debug];
368+
#endif
367369
NSString *appPackageName = [[NSBundle mainBundle] executablePath];
368370

369371
NSMutableSet<NSString *> *_Nonnull imagesAddrToRetrieveDebugMetaImages =
@@ -451,7 +453,7 @@ - (NSDictionary *)fetchNativeStackFramesBy:(NSArray<NSNumber *> *)instructionsAd
451453
__block NSMutableDictionary<NSString *, id> *serializedScope;
452454
// Temp work around until sorted out this API in sentry-cocoa.
453455
// TODO: If the callback isnt' executed the promise wouldn't be resolved.
454-
[SentrySDK configureScope:^(SentryScope *_Nonnull scope) {
456+
[SentrySDKWrapper configureScope:^(SentryScope *_Nonnull scope) {
455457
serializedScope = [[scope serialize] mutableCopy];
456458

457459
NSDictionary<NSString *, id> *user = [serializedScope valueForKey:@"user"];
@@ -655,7 +657,7 @@ - (NSDictionary *)fetchNativeStackFramesBy:(NSArray<NSNumber *> *)instructionsAd
655657

656658
RCT_EXPORT_METHOD(setUser : (NSDictionary *)userKeys otherUserKeys : (NSDictionary *)userDataKeys)
657659
{
658-
[SentrySDK configureScope:^(SentryScope *_Nonnull scope) {
660+
[SentrySDKWrapper configureScope:^(SentryScope *_Nonnull scope) {
659661
[scope setUser:[RNSentry userFrom:userKeys otherUserKeys:userDataKeys]];
660662
}];
661663
}
@@ -704,7 +706,7 @@ + (SentryUser *_Nullable)userFrom:(NSDictionary *)userKeys
704706

705707
RCT_EXPORT_METHOD(addBreadcrumb : (NSDictionary *)breadcrumb)
706708
{
707-
[SentrySDK configureScope:^(SentryScope *_Nonnull scope) {
709+
[SentrySDKWrapper configureScope:^(SentryScope *_Nonnull scope) {
708710
[scope addBreadcrumb:[RNSentryBreadcrumb from:breadcrumb]];
709711
}];
710712

@@ -718,12 +720,12 @@ + (SentryUser *_Nullable)userFrom:(NSDictionary *)userKeys
718720

719721
RCT_EXPORT_METHOD(clearBreadcrumbs)
720722
{
721-
[SentrySDK configureScope:^(SentryScope *_Nonnull scope) { [scope clearBreadcrumbs]; }];
723+
[SentrySDKWrapper configureScope:^(SentryScope *_Nonnull scope) { [scope clearBreadcrumbs]; }];
722724
}
723725

724726
RCT_EXPORT_METHOD(setExtra : (NSString *)key extra : (NSString *)extra)
725727
{
726-
[SentrySDK
728+
[SentrySDKWrapper
727729
configureScope:^(SentryScope *_Nonnull scope) { [scope setExtraValue:extra forKey:key]; }];
728730
}
729731

@@ -733,7 +735,7 @@ + (SentryUser *_Nullable)userFrom:(NSDictionary *)userKeys
733735
return;
734736
}
735737

736-
[SentrySDK configureScope:^(SentryScope *_Nonnull scope) {
738+
[SentrySDKWrapper configureScope:^(SentryScope *_Nonnull scope) {
737739
if (context == nil) {
738740
[scope removeContextForKey:key];
739741
} else {
@@ -744,17 +746,17 @@ + (SentryUser *_Nullable)userFrom:(NSDictionary *)userKeys
744746

745747
RCT_EXPORT_METHOD(setTag : (NSString *)key value : (NSString *)value)
746748
{
747-
[SentrySDK
749+
[SentrySDKWrapper
748750
configureScope:^(SentryScope *_Nonnull scope) { [scope setTagValue:value forKey:key]; }];
749751
}
750752

751-
RCT_EXPORT_METHOD(crash) { [SentrySDK crash]; }
753+
RCT_EXPORT_METHOD(crash) { [SentrySDKWrapper crash]; }
752754

753755
RCT_EXPORT_METHOD(closeNativeSdk
754756
: (RCTPromiseResolveBlock)resolve rejecter
755757
: (RCTPromiseRejectBlock)reject)
756758
{
757-
[SentrySDK close];
759+
[SentrySDKWrapper close];
758760
resolve(@YES);
759761
}
760762

@@ -957,7 +959,7 @@ + (SentryUser *_Nullable)userFrom:(NSDictionary *)userKeys
957959
: (RCTPromiseResolveBlock)resolve rejecter
958960
: (RCTPromiseRejectBlock)reject)
959961
{
960-
resolve(@([SentrySDK crashedLastRun]));
962+
resolve(@([SentrySDKWrapper crashedLastRun]));
961963
}
962964

963965
// Thanks to this guard, we won't compile this code when we build for the old architecture.

packages/core/ios/RNSentryReplayBreadcrumbConverter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@import Sentry;
22

33
#if SENTRY_TARGET_REPLAY_SUPPORTED
4-
@class SentryRRWebEvent;
4+
@protocol SentryRRWebEvent;
55

66
@interface RNSentryReplayBreadcrumbConverter : NSObject <SentryReplayBreadcrumbConverter>
77

packages/core/ios/SentrySDKWrapper.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#import <Foundation/Foundation.h>
2+
3+
@class SentryOptions;
4+
@class SentryScope;
5+
6+
@interface SentrySDKWrapper : NSObject
7+
8+
+ (void)configureScope:(void (^)(SentryScope *scope))callback;
9+
10+
+ (void)crash;
11+
12+
+ (void)close;
13+
14+
+ (BOOL)crashedLastRun;
15+
16+
+ (void)startWithOptions:(SentryOptions *)options;
17+
18+
@end

packages/core/ios/SentrySDKWrapper.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#import "SentrySDKWrapper.h"
2+
@import Sentry;
3+
4+
@implementation SentrySDKWrapper
5+
6+
+ (void)startWithOptions:(SentryOptions *)options
7+
{
8+
[SentrySDK startWithOptions:options];
9+
}
10+
11+
+ (void)crash
12+
{
13+
[SentrySDK crash];
14+
}
15+
16+
+ (void)close
17+
{
18+
[SentrySDK close];
19+
}
20+
21+
+ (BOOL)crashedLastRun
22+
{
23+
return [SentrySDK crashedLastRun];
24+
}
25+
26+
+ (void)configureScope:(void (^)(SentryScope *scope))callback
27+
{
28+
[SentrySDK configureScope:callback];
29+
}
30+
31+
@end

0 commit comments

Comments
 (0)