Skip to content

Commit 4840e63

Browse files
committed
ref: Move options to wrapper
1 parent d1bfbde commit 4840e63

File tree

7 files changed

+359
-264
lines changed

7 files changed

+359
-264
lines changed

packages/core/RNSentryCocoaTester/RNSentryCocoaTester.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
33F58ACF2977037D008F60EA /* RNSentryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNSentryTests.m; sourceTree = "<group>"; };
5353
650CB718ACFBD05609BF2126 /* libPods-RNSentryCocoaTesterTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-RNSentryCocoaTesterTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
5454
E2321E7CFA55AB617247098E /* Pods-RNSentryCocoaTesterTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RNSentryCocoaTesterTests.debug.xcconfig"; path = "Target Support Files/Pods-RNSentryCocoaTesterTests/Pods-RNSentryCocoaTesterTests.debug.xcconfig"; sourceTree = "<group>"; };
55+
FADF868E2EBD053E00D6652D /* SentrySDKWrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentrySDKWrapper.h; path = ../ios/SentrySDKWrapper.h; sourceTree = SOURCE_ROOT; };
5556
/* End PBXFileReference section */
5657

5758
/* Begin PBXFrameworksBuildPhase section */
@@ -136,6 +137,7 @@
136137
3360843A2C32E3A8008CC412 /* RNSentryReplayBreadcrumbConverter.h */,
137138
330F308D2C0F385A002A0D4E /* RNSentryBreadcrumb.h */,
138139
33958C672BFCEF5A00AD1FB6 /* RNSentryOnDrawReporter.h */,
140+
FADF868E2EBD053E00D6652D /* SentrySDKWrapper.h */,
139141
33AFE0132B8F31AF00AAB120 /* RNSentryDependencyContainer.h */,
140142
338739072A7D7D2800950DDD /* RNSentryReplay.h */,
141143
);
@@ -236,10 +238,14 @@
236238
inputFileListPaths = (
237239
"${PODS_ROOT}/Target Support Files/Pods-RNSentryCocoaTesterTests/Pods-RNSentryCocoaTesterTests-resources-${CONFIGURATION}-input-files.xcfilelist",
238240
);
241+
inputPaths = (
242+
);
239243
name = "[CP] Copy Pods Resources";
240244
outputFileListPaths = (
241245
"${PODS_ROOT}/Target Support Files/Pods-RNSentryCocoaTesterTests/Pods-RNSentryCocoaTesterTests-resources-${CONFIGURATION}-output-files.xcfilelist",
242246
);
247+
outputPaths = (
248+
);
243249
runOnlyForDeploymentPostprocessing = 0;
244250
shellPath = /bin/sh;
245251
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-RNSentryCocoaTesterTests/Pods-RNSentryCocoaTesterTests-resources.sh\"\n";

packages/core/RNSentryCocoaTester/RNSentryCocoaTesterTests/RNSentryTests.m

Lines changed: 143 additions & 100 deletions
Large diffs are not rendered by default.

packages/core/ios/RNSentry+fetchNativeStack.m

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
#import "RNSentryBreadcrumb.h"
33
#import "RNSentryId.h"
44
#import <Sentry/PrivateSentrySDKOnly.h>
5-
#import <Sentry/SentryAppStartMeasurement.h>
6-
#import <Sentry/SentryBreadcrumb.h>
7-
#import <Sentry/SentryDebugMeta.h>
8-
#import <Sentry/SentryEvent.h>
9-
#import <Sentry/SentryException.h>
10-
#import <Sentry/SentryFormatter.h>
11-
#import <Sentry/SentryOptions.h>
12-
#import <Sentry/SentryUser.h>
135
@import Sentry;
146

157
// This method was moved to a new category so we can use `@import Sentry` to use Sentry's Swift

packages/core/ios/RNSentry.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ typedef int (*SymbolicateCallbackType)(const void *, Dl_info *);
2020

2121
@interface RNSentry : RCTEventEmitter <RCTBridgeModule>
2222

23-
- (SentryOptions *_Nullable)createOptionsWithDictionary:(NSDictionary *_Nonnull)options
24-
error:(NSError *_Nullable *_Nonnull)errorPointer;
23+
- (NSMutableDictionary *)prepareOptions:(NSDictionary *)options;
2524

2625
- (void)setEventOriginTag:(SentryEvent *)event;
2726

packages/core/ios/RNSentry.mm

Lines changed: 58 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
#import <Sentry/SentryException.h>
2727
#import <Sentry/SentryFormatter.h>
2828
#import <Sentry/SentryGeo.h>
29-
#import <Sentry/SentryOptions.h>
30-
#import <Sentry/SentryOptionsInternal.h>
3129
#import <Sentry/SentryScreenFrames.h>
3230
#import <Sentry/SentryUser.h>
3331

@@ -85,24 +83,68 @@ - (instancetype)init
8583
return self;
8684
}
8785

86+
- (NSMutableDictionary *)prepareOptions:(NSDictionary *)options
87+
{
88+
SentryBeforeSendEventCallback beforeSend = ^SentryEvent *(SentryEvent *event) {
89+
// We don't want to send an event after startup that came from a Unhandled JS Exception of
90+
// React Native because we sent it already before the app crashed.
91+
if (nil != event.exceptions.firstObject.type &&
92+
[event.exceptions.firstObject.type rangeOfString:@"Unhandled JS Exception"].location
93+
!= NSNotFound) {
94+
return nil;
95+
}
96+
97+
// Regex and Str are set when one of them has value so we only need to check one of them.
98+
if (self->_ignoreErrorPatternsStr || self->_ignoreErrorPatternsRegex) {
99+
for (SentryException *exception in event.exceptions) {
100+
if ([self shouldIgnoreError:exception.value]) {
101+
return nil;
102+
}
103+
}
104+
if ([self shouldIgnoreError:event.message.message]) {
105+
return nil;
106+
}
107+
}
108+
109+
[self setEventOriginTag:event];
110+
return event;
111+
};
112+
113+
NSMutableDictionary *mutableOptions = [options mutableCopy];
114+
[mutableOptions setValue:beforeSend forKey:@"beforeSend"];
115+
116+
// remove performance traces sample rate and traces sampler since we don't want to synchronize
117+
// these configurations to the Native SDKs. The user could tho initialize the SDK manually and
118+
// set themselves.
119+
[mutableOptions removeObjectForKey:@"tracesSampleRate"];
120+
[mutableOptions removeObjectForKey:@"tracesSampler"];
121+
[mutableOptions removeObjectForKey:@"enableTracing"];
122+
123+
[self trySetIgnoreErrors:mutableOptions];
124+
125+
return mutableOptions;
126+
}
127+
88128
RCT_EXPORT_MODULE()
89129
RCT_EXPORT_METHOD(initNativeSdk : (NSDictionary *_Nonnull)options resolve : (
90130
RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
91131
{
132+
NSMutableDictionary *mutableOptions = [self prepareOptions:options];
133+
#if SENTRY_TARGET_REPLAY_SUPPORTED
134+
BOOL isSessionReplayEnabled = [RNSentryReplay updateOptions:mutableOptions];
135+
#else
136+
// Defaulting to false for unsupported targets
137+
BOOL isSessionReplayEnabled = NO;
138+
#endif
92139
NSError *error = nil;
93-
SentryOptions *sentryOptions = [self createOptionsWithDictionary:options error:&error];
140+
[SentrySDKWrapper setupWithDictionary:mutableOptions
141+
isSessionReplayEnabled:isSessionReplayEnabled
142+
error:&error];
94143
if (error != nil) {
95144
reject(@"SentryReactNative", error.localizedDescription, error);
96145
return;
97146
}
98147

99-
NSString *sdkVersion = [PrivateSentrySDKOnly getSdkVersionString];
100-
[PrivateSentrySDKOnly setSdkName:NATIVE_SDK_NAME andVersionString:sdkVersion];
101-
[PrivateSentrySDKOnly addSdkPackage:REACT_NATIVE_SDK_PACKAGE_NAME
102-
version:REACT_NATIVE_SDK_PACKAGE_VERSION];
103-
104-
[SentrySDKWrapper startWithOptions:sentryOptions];
105-
106148
#if TARGET_OS_IPHONE || TARGET_OS_MACCATALYST
107149
BOOL appIsActive =
108150
[[UIApplication sharedApplication] applicationState] == UIApplicationStateActive;
@@ -113,8 +155,8 @@ - (instancetype)init
113155
// If the app is active/in foreground, and we have not sent the SentryHybridSdkDidBecomeActive
114156
// notification, send it.
115157
if (appIsActive && !sentHybridSdkDidBecomeActive
116-
&& (PrivateSentrySDKOnly.options.enableAutoSessionTracking
117-
|| PrivateSentrySDKOnly.options.enableWatchdogTerminationTracking)) {
158+
&& ([SentrySDKWrapper enableAutoSessionTracking] ||
159+
[SentrySDKWrapper enableWatchdogTerminationTracking])) {
118160
[[NSNotificationCenter defaultCenter] postNotificationName:@"SentryHybridSdkDidBecomeActive"
119161
object:nil];
120162

@@ -128,7 +170,7 @@ - (instancetype)init
128170
resolve(@YES);
129171
}
130172

131-
- (void)trySetIgnoreErrors:(NSMutableDictionary *)options
173+
- (void)trySetIgnoreErrors:(NSDictionary *)options
132174
{
133175
NSArray *ignoreErrorsStr = nil;
134176
NSArray *ignoreErrorsRegex = nil;
@@ -194,144 +236,6 @@ - (BOOL)shouldIgnoreError:(NSString *)message
194236
return NO;
195237
}
196238

197-
- (SentryOptions *_Nullable)createOptionsWithDictionary:(NSDictionary *_Nonnull)options
198-
error:(NSError *_Nonnull *_Nonnull)errorPointer
199-
{
200-
SentryBeforeSendEventCallback beforeSend = ^SentryEvent *(SentryEvent *event) {
201-
// We don't want to send an event after startup that came from a Unhandled JS Exception of
202-
// React Native because we sent it already before the app crashed.
203-
if (nil != event.exceptions.firstObject.type &&
204-
[event.exceptions.firstObject.type rangeOfString:@"Unhandled JS Exception"].location
205-
!= NSNotFound) {
206-
return nil;
207-
}
208-
209-
// Regex and Str are set when one of them has value so we only need to check one of them.
210-
if (self->_ignoreErrorPatternsStr || self->_ignoreErrorPatternsRegex) {
211-
for (SentryException *exception in event.exceptions) {
212-
if ([self shouldIgnoreError:exception.value]) {
213-
return nil;
214-
}
215-
}
216-
if ([self shouldIgnoreError:event.message.message]) {
217-
return nil;
218-
}
219-
}
220-
221-
[self setEventOriginTag:event];
222-
return event;
223-
};
224-
225-
NSMutableDictionary *mutableOptions = [options mutableCopy];
226-
[mutableOptions setValue:beforeSend forKey:@"beforeSend"];
227-
228-
// remove performance traces sample rate and traces sampler since we don't want to synchronize
229-
// these configurations to the Native SDKs. The user could tho initialize the SDK manually and
230-
// set themselves.
231-
[mutableOptions removeObjectForKey:@"tracesSampleRate"];
232-
[mutableOptions removeObjectForKey:@"tracesSampler"];
233-
[mutableOptions removeObjectForKey:@"enableTracing"];
234-
235-
#if SENTRY_TARGET_REPLAY_SUPPORTED
236-
BOOL isSessionReplayEnabled = [RNSentryReplay updateOptions:mutableOptions];
237-
#else
238-
// Defaulting to false for unsupported targets
239-
BOOL isSessionReplayEnabled = NO;
240-
#endif
241-
242-
SentryOptions *sentryOptions = [SentryOptionsInternal initWithDict:mutableOptions
243-
didFailWithError:errorPointer];
244-
if (*errorPointer != nil) {
245-
return nil;
246-
}
247-
248-
// Exclude Dev Server and Sentry Dsn request from Breadcrumbs
249-
NSString *dsn = [self getURLFromDSN:[mutableOptions valueForKey:@"dsn"]];
250-
NSString *devServerUrl = [mutableOptions valueForKey:@"devServerUrl"];
251-
sentryOptions.beforeBreadcrumb
252-
= ^SentryBreadcrumb *_Nullable(SentryBreadcrumb *_Nonnull breadcrumb)
253-
{
254-
NSString *url = breadcrumb.data[@"url"] ?: @"";
255-
256-
if ([@"http" isEqualToString:breadcrumb.type]
257-
&& ((dsn != nil && [url hasPrefix:dsn])
258-
|| (devServerUrl != nil && [url hasPrefix:devServerUrl]))) {
259-
return nil;
260-
}
261-
return breadcrumb;
262-
};
263-
264-
if ([mutableOptions valueForKey:@"enableNativeCrashHandling"] != nil) {
265-
BOOL enableNativeCrashHandling = [mutableOptions[@"enableNativeCrashHandling"] boolValue];
266-
267-
if (!enableNativeCrashHandling) {
268-
sentryOptions.enableCrashHandler = NO;
269-
}
270-
}
271-
272-
// Set spotlight option
273-
if ([mutableOptions valueForKey:@"spotlight"] != nil) {
274-
id spotlightValue = [mutableOptions valueForKey:@"spotlight"];
275-
if ([spotlightValue isKindOfClass:[NSString class]]) {
276-
NSLog(@"Using Spotlight on address: %@", spotlightValue);
277-
sentryOptions.enableSpotlight = true;
278-
sentryOptions.spotlightUrl = spotlightValue;
279-
} else if ([spotlightValue isKindOfClass:[NSNumber class]]) {
280-
sentryOptions.enableSpotlight = [spotlightValue boolValue];
281-
id defaultSpotlightUrl = [mutableOptions valueForKey:@"defaultSidecarUrl"];
282-
if (defaultSpotlightUrl != nil) {
283-
sentryOptions.spotlightUrl = defaultSpotlightUrl;
284-
}
285-
}
286-
}
287-
288-
if ([mutableOptions valueForKey:@"enableLogs"] != nil) {
289-
id enableLogsValue = [mutableOptions valueForKey:@"enableLogs"];
290-
if ([enableLogsValue isKindOfClass:[NSNumber class]]) {
291-
[RNSentryExperimentalOptions setEnableLogs:[enableLogsValue boolValue]
292-
sentryOptions:sentryOptions];
293-
}
294-
}
295-
[self trySetIgnoreErrors:mutableOptions];
296-
297-
// Enable the App start and Frames tracking measurements
298-
if ([mutableOptions valueForKey:@"enableAutoPerformanceTracing"] != nil) {
299-
BOOL enableAutoPerformanceTracing =
300-
[mutableOptions[@"enableAutoPerformanceTracing"] boolValue];
301-
PrivateSentrySDKOnly.appStartMeasurementHybridSDKMode = enableAutoPerformanceTracing;
302-
#if TARGET_OS_IPHONE || TARGET_OS_MACCATALYST
303-
PrivateSentrySDKOnly.framesTrackingMeasurementHybridSDKMode = enableAutoPerformanceTracing;
304-
#endif
305-
}
306-
307-
// Failed requests can only be enabled in one SDK to avoid duplicates
308-
sentryOptions.enableCaptureFailedRequests = NO;
309-
310-
NSDictionary *experiments = options[@"_experiments"];
311-
if (experiments != nil && [experiments isKindOfClass:[NSDictionary class]]) {
312-
BOOL enableUnhandledCPPExceptions =
313-
[experiments[@"enableUnhandledCPPExceptionsV2"] boolValue];
314-
[RNSentryExperimentalOptions setEnableUnhandledCPPExceptionsV2:enableUnhandledCPPExceptions
315-
sentryOptions:sentryOptions];
316-
}
317-
318-
if (isSessionReplayEnabled) {
319-
[RNSentryExperimentalOptions setEnableSessionReplayInUnreliableEnvironment:YES
320-
sentryOptions:sentryOptions];
321-
}
322-
323-
return sentryOptions;
324-
}
325-
326-
- (NSString *_Nullable)getURLFromDSN:(NSString *)dsn
327-
{
328-
NSURL *url = [NSURL URLWithString:dsn];
329-
if (!url) {
330-
return nil;
331-
}
332-
return [NSString stringWithFormat:@"%@://%@", url.scheme, url.host];
333-
}
334-
335239
- (void)setEventOriginTag:(SentryEvent *)event
336240
{
337241
if (event.sdk != nil) {
@@ -460,7 +364,7 @@ - (void)stopObserving
460364
contexts[@"os"] = os;
461365
}
462366

463-
NSString *releaseName = SentrySDKInternal.options.releaseName;
367+
NSString *releaseName = [SentrySDKWrapper releaseName];
464368
if (releaseName) {
465369
contexts[@"release"] = releaseName;
466370
}
@@ -492,7 +396,7 @@ - (void)stopObserving
492396
RCT_EXPORT_METHOD(fetchNativeDeviceContexts : (RCTPromiseResolveBlock)resolve rejecter : (
493397
RCTPromiseRejectBlock)reject)
494398
{
495-
if (PrivateSentrySDKOnly.options.debug) {
399+
if ([SentrySDKWrapper debug]) {
496400
NSLog(@"Bridge call to: deviceContexts");
497401
}
498402
__block NSMutableDictionary<NSString *, id> *serializedScope;
@@ -507,7 +411,7 @@ - (void)stopObserving
507411
forKey:@"user"];
508412
}
509413

510-
if (PrivateSentrySDKOnly.options.debug) {
414+
if ([SentrySDKWrapper debug]) {
511415
NSData *data = [NSJSONSerialization dataWithJSONObject:serializedScope
512416
options:0
513417
error:nil];

packages/core/ios/SentrySDKWrapper.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,20 @@
1515

1616
+ (void)startWithOptions:(SentryOptions *)options;
1717

18+
+ (SentryOptions *)createOptionsWithDictionary:(NSDictionary *)options
19+
isSessionReplayEnabled:(BOOL)isSessionReplayEnabled
20+
error:(NSError **)errorPointer;
21+
22+
+ (void)setupWithDictionary:(NSDictionary *)options
23+
isSessionReplayEnabled:(BOOL)isSessionReplayEnabled
24+
error:(NSError **)errorPointer;
25+
26+
+ (BOOL)debug;
27+
28+
+ (NSString *)releaseName;
29+
30+
+ (BOOL)enableAutoSessionTracking;
31+
32+
+ (BOOL)enableWatchdogTerminationTracking;
33+
1834
@end

0 commit comments

Comments
 (0)