Skip to content

Commit

Permalink
Fix: SentryOptions initWithDict return nil for error (#2679)
Browse files Browse the repository at this point in the history
Changed [SentryOptions initWithDict:error:] to return nil if an error occurs even if no error argument is provided.
Before this, it only returned nil if and error argument was provided.
  • Loading branch information
brustolin authored Mar 7, 2023
1 parent d253cdf commit d40512b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryDsn.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ - (_Nullable instancetype)initWithString:(NSString *)dsnString
self = [super init];
if (self) {
_url = [self convertDsnString:dsnString didFailWithError:error];
if (nil != error && nil != *error) {
if (_url == nil) {
return nil;
}
}
Expand Down
29 changes: 16 additions & 13 deletions Sources/Sentry/SentryOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ - (_Nullable instancetype)initWithDict:(NSDictionary<NSString *, id> *)options
{
if (self = [self init]) {
if (![self validateOptions:options didFailWithError:error]) {
[SentryLog
logWithMessage:[NSString stringWithFormat:@"Failed to initialize: %@", *error]
andLevel:kSentryLevelError];
if (error != nil) {
SENTRY_LOG_ERROR(@"Failed to initialize SentryOptions: %@", *error);
} else {
SENTRY_LOG_ERROR(@"Failed to initialize SentryOptions");
}
return nil;
}
}
Expand Down Expand Up @@ -240,12 +242,17 @@ - (BOOL)validateOptions:(NSDictionary<NSString *, id> *)options
}
}

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

self.parsedDsn = [[SentryDsn alloc] initWithString:dsn didFailWithError:error];
self.parsedDsn = [[SentryDsn alloc] initWithString:dsn didFailWithError:error];
if (self.parsedDsn == nil) {
return NO;
}
}

if ([options[@"release"] isKindOfClass:[NSString class]]) {
self.releaseName = options[@"release"];
Expand Down Expand Up @@ -433,11 +440,7 @@ - (BOOL)validateOptions:(NSDictionary<NSString *, id> *)options
}
#endif

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

- (void)setBool:(id)value block:(void (^)(BOOL))block
Expand Down
6 changes: 6 additions & 0 deletions Tests/SentryTests/Networking/SentryDsnTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ - (void)testGetStoreDsnCachesResult
XCTAssertTrue([dsn getStoreEndpoint] == [dsn getStoreEndpoint]);
}

- (void)testInitWithInvalidString
{
SentryDsn *dsn = [[SentryDsn alloc] initWithString:@"This is invalid DSN" didFailWithError:nil];
XCTAssertNil(dsn);
}

- (void)testGetEnvelopeDsnCachesResult
{
SentryDsn *dsn = [[SentryDsn alloc] initWithString:@"https://username:[email protected]/1"
Expand Down
9 changes: 8 additions & 1 deletion Tests/SentryTests/SentryOptionsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ - (void)testInvalidDsn
XCTAssertNil(options);
}

- (void)testInvalidDsnWithNoErrorArgument
{
SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ @"dsn" : @"https://sentry.io" }
didFailWithError:nil];
XCTAssertNil(options);
}

- (void)testRelease
{
SentryOptions *options = [self getValidOptions:@{ @"release" : @"abc" }];
Expand Down Expand Up @@ -531,7 +538,7 @@ - (void)testNSNull_SetsDefaultValue
}
didFailWithError:nil];

XCTAssertNotNil(options.parsedDsn);
XCTAssertNil(options.parsedDsn);
[self assertDefaultValues:options];
}

Expand Down

0 comments on commit d40512b

Please sign in to comment.