Skip to content

Commit

Permalink
Merge 4a24994 into d975745
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrenskers authored Nov 18, 2022
2 parents d975745 + 4a24994 commit bd0eaeb
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 1,139 deletions.
16 changes: 2 additions & 14 deletions Sources/Sentry/Public/SentryOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ NS_ASSUME_NONNULL_BEGIN
NS_SWIFT_NAME(Options)
@interface SentryOptions : NSObject

/**
* Init SentryOptions.
* @param options Options dictionary
* @return SentryOptions
*/
- (_Nullable instancetype)initWithDict:(NSDictionary<NSString *, id> *)options
didFailWithError:(NSError *_Nullable *_Nullable)error;
- (_Nullable instancetype)initWithDsn:(NSString *)dsn
didFailWithError:(NSError *_Nullable *_Nullable)error;

/**
* The DSN tells the SDK where to send the events to. If this value is not provided, the SDK will
Expand Down Expand Up @@ -164,13 +159,6 @@ NS_SWIFT_NAME(Options)
*/
@property (nonatomic, assign) BOOL stitchAsyncCode;

/**
* Describes the Sentry SDK and its configuration used to capture and transmit an event.
* This is reserved for internal use, and will be removed in a future version of the SDK.
*/
@property (nonatomic, readonly, strong) SentrySdkInfo *sdkInfo DEPRECATED_MSG_ATTRIBUTE(
"This property will be removed in a future version of the SDK");

/**
* The maximum size for each attachment in bytes. Default is 20 MiB / 20 * 1024 * 1024 bytes.
*
Expand Down
5 changes: 0 additions & 5 deletions Sources/Sentry/Public/SentrySDK.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ SENTRY_NO_INIT
*/
@property (class, nonatomic, readonly) BOOL isEnabled;

/**
* Inits and configures Sentry (SentryHub, SentryClient) and sets up all integrations.
*/
+ (void)startWithOptions:(NSDictionary<NSString *, id> *)optionsDict NS_SWIFT_NAME(start(options:));

/**
* Inits and configures Sentry (SentryHub, SentryClient) and sets up all integrations.
*/
Expand Down
235 changes: 6 additions & 229 deletions Sources/Sentry/SentryOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ - (instancetype)init
return self;
}

- (_Nullable instancetype)initWithDict:(NSDictionary<NSString *, id> *)options
didFailWithError:(NSError *_Nullable *_Nullable)error
- (_Nullable instancetype)initWithDsn:(NSString *)dsn
didFailWithError:(NSError *_Nullable *_Nullable)error
{
if (self = [self init]) {
if (![self validateOptions:options didFailWithError:error]) {
[SentryLog
logWithMessage:[NSString stringWithFormat:@"Failed to initialize: %@", *error]
andLevel:kSentryLevelError];
self.parsedDsn = [[SentryDsn alloc] initWithString:dsn didFailWithError:error];
self.dsn = dsn;

if (error != nil && *error != nil) {
return nil;
}
}
Expand Down Expand Up @@ -191,229 +191,6 @@ - (void)setDsn:(NSString *)dsn
}
}

/**
* Populates all `SentryOptions` values from `options` dict using fallbacks/defaults if needed.
*/
- (BOOL)validateOptions:(NSDictionary<NSString *, id> *)options
didFailWithError:(NSError *_Nullable *_Nullable)error
{
NSPredicate *isNSString = [NSPredicate predicateWithBlock:^BOOL(
id object, NSDictionary *bindings) { return [object isKindOfClass:[NSString class]]; }];

[self setBool:options[@"debug"] block:^(BOOL value) { self->_debug = value; }];

if ([options[@"diagnosticLevel"] isKindOfClass:[NSString class]]) {
for (SentryLevel level = 0; level <= kSentryLevelFatal; level++) {
if ([nameForSentryLevel(level) isEqualToString:options[@"diagnosticLevel"]]) {
self.diagnosticLevel = level;
break;
}
}
}

NSString *dsn = @"";
if (nil != options[@"dsn"] && [options[@"dsn"] isKindOfClass:[NSString class]]) {
dsn = options[@"dsn"];
}

self.parsedDsn = [[SentryDsn alloc] initWithString:dsn didFailWithError:error];

if ([options[@"release"] isKindOfClass:[NSString class]]) {
self.releaseName = options[@"release"];
}

if ([options[@"environment"] isKindOfClass:[NSString class]]) {
self.environment = options[@"environment"];
}

if ([options[@"dist"] isKindOfClass:[NSString class]]) {
self.dist = options[@"dist"];
}

[self setBool:options[@"enabled"] block:^(BOOL value) { self->_enabled = value; }];

[self setBool:options[@"enableCrashHandler"]
block:^(BOOL value) { self->_enableCrashHandler = value; }];

if ([options[@"maxBreadcrumbs"] isKindOfClass:[NSNumber class]]) {
self.maxBreadcrumbs = [options[@"maxBreadcrumbs"] unsignedIntValue];
}

[self setBool:options[@"enableNetworkBreadcrumbs"]
block:^(BOOL value) { self->_enableNetworkBreadcrumbs = value; }];

if ([options[@"maxCacheItems"] isKindOfClass:[NSNumber class]]) {
self.maxCacheItems = [options[@"maxCacheItems"] unsignedIntValue];
}

if ([self isBlock:options[@"beforeSend"]]) {
self.beforeSend = options[@"beforeSend"];
}

if ([self isBlock:options[@"beforeBreadcrumb"]]) {
self.beforeBreadcrumb = options[@"beforeBreadcrumb"];
}

if ([self isBlock:options[@"onCrashedLastRun"]]) {
self.onCrashedLastRun = options[@"onCrashedLastRun"];
}

if ([options[@"integrations"] isKindOfClass:[NSArray class]]) {
self.integrations = [options[@"integrations"] filteredArrayUsingPredicate:isNSString];
}

if ([options[@"sampleRate"] isKindOfClass:[NSNumber class]]) {
self.sampleRate = options[@"sampleRate"];
}

[self setBool:options[@"enableAutoSessionTracking"]
block:^(BOOL value) { self->_enableAutoSessionTracking = value; }];

[self setBool:options[@"enableOutOfMemoryTracking"]
block:^(BOOL value) { self->_enableOutOfMemoryTracking = value; }];

if ([options[@"sessionTrackingIntervalMillis"] isKindOfClass:[NSNumber class]]) {
self.sessionTrackingIntervalMillis =
[options[@"sessionTrackingIntervalMillis"] unsignedIntValue];
}

[self setBool:options[@"attachStacktrace"]
block:^(BOOL value) { self->_attachStacktrace = value; }];

[self setBool:options[@"stitchAsyncCode"]
block:^(BOOL value) { self->_stitchAsyncCode = value; }];

if ([options[@"maxAttachmentSize"] isKindOfClass:[NSNumber class]]) {
self.maxAttachmentSize = [options[@"maxAttachmentSize"] unsignedIntValue];
}

[self setBool:options[@"sendDefaultPii"]
block:^(BOOL value) { self->_sendDefaultPii = value; }];

[self setBool:options[@"enableAutoPerformanceTracking"]
block:^(BOOL value) { self->_enableAutoPerformanceTracking = value; }];

[self setBool:options[@"enableCaptureFailedRequests"]
block:^(BOOL value) { self->_enableCaptureFailedRequests = value; }];

#if SENTRY_HAS_UIKIT
[self setBool:options[@"enableUIViewControllerTracking"]
block:^(BOOL value) { self->_enableUIViewControllerTracking = value; }];

[self setBool:options[@"attachScreenshot"]
block:^(BOOL value) { self->_attachScreenshot = value; }];

[self setBool:options[@"attachViewHierarchy"]
block:^(BOOL value) { self->_attachViewHierarchy = value; }];

[self setBool:options[@"enableUserInteractionTracing"]
block:^(BOOL value) { self->_enableUserInteractionTracing = value; }];

if ([options[@"idleTimeout"] isKindOfClass:[NSNumber class]]) {
self.idleTimeout = [options[@"idleTimeout"] doubleValue];
}

[self setBool:options[@"enablePreWarmedAppStartTracking"]
block:^(BOOL value) { self->_enablePreWarmedAppStartTracking = value; }];
#endif

[self setBool:options[@"enableAppHangTracking"]
block:^(BOOL value) { self->_enableAppHangTracking = value; }];

if ([options[@"appHangTimeoutInterval"] isKindOfClass:[NSNumber class]]) {
self.appHangTimeoutInterval = [options[@"appHangTimeoutInterval"] doubleValue];
}

[self setBool:options[@"enableNetworkTracking"]
block:^(BOOL value) { self->_enableNetworkTracking = value; }];

[self setBool:options[@"enableFileIOTracking"]
block:^(BOOL value) { self->_enableFileIOTracking = value; }];

if ([options[@"tracesSampleRate"] isKindOfClass:[NSNumber class]]) {
self.tracesSampleRate = options[@"tracesSampleRate"];
}

if ([self isBlock:options[@"tracesSampler"]]) {
self.tracesSampler = options[@"tracesSampler"];
}

if ([options[@"inAppIncludes"] isKindOfClass:[NSArray class]]) {
NSArray<NSString *> *inAppIncludes =
[options[@"inAppIncludes"] filteredArrayUsingPredicate:isNSString];
_inAppIncludes = [_inAppIncludes arrayByAddingObjectsFromArray:inAppIncludes];
}

if ([options[@"inAppExcludes"] isKindOfClass:[NSArray class]]) {
_inAppExcludes = [options[@"inAppExcludes"] filteredArrayUsingPredicate:isNSString];
}

if ([options[@"urlSessionDelegate"] conformsToProtocol:@protocol(NSURLSessionDelegate)]) {
self.urlSessionDelegate = options[@"urlSessionDelegate"];
}

[self setBool:options[@"enableSwizzling"]
block:^(BOOL value) { self->_enableSwizzling = value; }];

[self setBool:options[@"enableCoreDataTracking"]
block:^(BOOL value) { self->_enableCoreDataTracking = value; }];

#if SENTRY_TARGET_PROFILING_SUPPORTED
if ([options[@"profilesSampleRate"] isKindOfClass:[NSNumber class]]) {
self.profilesSampleRate = options[@"profilesSampleRate"];
}

if ([self isBlock:options[@"profilesSampler"]]) {
self.profilesSampler = options[@"profilesSampler"];
}

[self setBool:options[@"enableProfiling"]
block:^(BOOL value) { self->_enableProfiling = value; }];
#endif

[self setBool:options[@"sendClientReports"]
block:^(BOOL value) { self->_sendClientReports = value; }];

[self setBool:options[@"enableAutoBreadcrumbTracking"]
block:^(BOOL value) { self->_enableAutoBreadcrumbTracking = value; }];

if ([options[@"tracePropagationTargets"] isKindOfClass:[NSArray class]]) {
self.tracePropagationTargets = options[@"tracePropagationTargets"];
}

if ([options[@"failedRequestStatusCodes"] isKindOfClass:[NSArray class]]) {
self.failedRequestStatusCodes = options[@"failedRequestStatusCodes"];
}

if ([options[@"failedRequestTargets"] isKindOfClass:[NSArray class]]) {
self.failedRequestTargets = options[@"failedRequestTargets"];
}

// SentrySdkInfo already expects a dictionary with {"sdk": {"name": ..., "value": ...}}
// so we're passing the whole options object.
// Note: we should remove this code once the hybrid SDKs move over to the new
// PrivateSentrySDKOnly setter functions.
if ([options[@"sdk"] isKindOfClass:[NSDictionary class]]) {
SentrySdkInfo *defaults = [[SentrySdkInfo alloc] initWithName:SentryMeta.sdkName
andVersion:SentryMeta.versionString];
SentrySdkInfo *sdkInfo = [[SentrySdkInfo alloc] initWithDict:options orDefaults:defaults];
SentryMeta.versionString = sdkInfo.version;
SentryMeta.sdkName = sdkInfo.name;
}

if (nil != error && nil != *error) {
return NO;
} else {
return YES;
}
}

- (SentrySdkInfo *)sdkInfo
{
return [[SentrySdkInfo alloc] initWithName:SentryMeta.sdkName
andVersion:SentryMeta.versionString];
}

- (void)setBool:(id)value block:(void (^)(BOOL))block
{
// Entries in the dictionary can be NSNull. Especially, on React-Native, this can happen.
Expand Down
13 changes: 0 additions & 13 deletions Sources/Sentry/SentrySDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,6 @@ + (void)setStartInvocations:(NSUInteger)value
startInvocations = value;
}

+ (void)startWithOptions:(NSDictionary<NSString *, id> *)optionsDict
{
NSError *error = nil;
SentryOptions *options = [[SentryOptions alloc] initWithDict:optionsDict
didFailWithError:&error];
if (nil != error) {
SENTRY_LOG_ERROR(@"Error while initializing the SDK");
SENTRY_LOG_ERROR(@"%@", error);
} else {
[SentrySDK startWithOptionsObject:options];
}
}

+ (void)startWithOptionsObject:(SentryOptions *)options
{
startInvocations++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ class SentryCrashIntegrationTests: NotificationCenterTestCase {
let releaseName = "1.0.0"
let dist = "14G60"
// The start of the SDK installs all integrations
SentrySDK.start(options: ["dsn": SentryCrashIntegrationTests.dsnAsString,
"release": releaseName,
"dist": dist]
)
SentrySDK.start { options in
options.dsn = SentryCrashIntegrationTests.dsnAsString
options.releaseName = releaseName
options.dist = dist
}

// To test this properly we need SentryCrash and SentryCrashIntegration installed and registered on the current hub of the SDK.

Expand Down
16 changes: 8 additions & 8 deletions Tests/SentryTests/Networking/SentryDsnTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ @implementation SentryDsnTests
- (void)testMissingUsernamePassword
{
NSError *error = nil;
SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ @"dsn" : @"https://sentry.io" }
didFailWithError:&error];
SentryOptions *options = [[SentryOptions alloc] initWithDsn:@"https://sentry.io"
didFailWithError:&error];
XCTAssertEqual(kSentryErrorInvalidDsnError, error.code);
XCTAssertNil(options);
}
Expand Down Expand Up @@ -62,26 +62,26 @@ - (void)testDsnHeaderUsername
- (void)testMissingScheme
{
NSError *error = nil;
SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ @"dsn" : @"sentry.io" }
didFailWithError:&error];
SentryOptions *options = [[SentryOptions alloc] initWithDsn:@"sentry.io"
didFailWithError:&error];
XCTAssertEqual(kSentryErrorInvalidDsnError, error.code);
XCTAssertNil(options);
}

- (void)testMissingHost
{
NSError *error = nil;
SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ @"dsn" : @"http:///1" }
didFailWithError:&error];
SentryOptions *options = [[SentryOptions alloc] initWithDsn:@"http:///1"
didFailWithError:&error];
XCTAssertEqual(kSentryErrorInvalidDsnError, error.code);
XCTAssertNil(options);
}

- (void)testUnsupportedProtocol
{
NSError *error = nil;
SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ @"dsn" : @"ftp://sentry.io/1" }
didFailWithError:&error];
SentryOptions *options = [[SentryOptions alloc] initWithDsn:@"ftp://sentry.io/1"
didFailWithError:&error];
XCTAssertEqual(kSentryErrorInvalidDsnError, error.code);
XCTAssertNil(options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SentryTransportInitializerTests: XCTestCase {
}

func testDefault() throws {
let options = try Options(dict: ["dsn": SentryTransportInitializerTests.dsnAsString])
let options = try Options(dsn: SentryTransportInitializerTests.dsnAsString)

let result = TransportInitializer.initTransport(options, sentryFileManager: fileManager)

Expand Down
4 changes: 1 addition & 3 deletions Tests/SentryTests/SentryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ class SentryClientTest: XCTestCase {
func getSut(configureOptions: (Options) -> Void = { _ in }) -> Client {
var client: Client!
do {
let options = try Options(dict: [
"dsn": SentryClientTest.dsn
])
let options = try Options(dsn: SentryClientTest.dsn)
configureOptions(options)

client = Client(
Expand Down
Loading

0 comments on commit bd0eaeb

Please sign in to comment.