diff --git a/CHANGELOG.md b/CHANGELOG.md index b131f4ed50..ae088fc86f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ This version adds a dependency on Swift. - Properly demangle Swift class name (#2162) +### Fixes + +- Errors shortly after SentrySDK.init now affect the session (#2430) + ### Breaking Changes - Remove `- [SentryOptions initWithDict:didFailWithError:]` (#2404) diff --git a/Sources/Sentry/SentryHub.m b/Sources/Sentry/SentryHub.m index 32982b9b8d..00c71093c7 100644 --- a/Sources/Sentry/SentryHub.m +++ b/Sources/Sentry/SentryHub.m @@ -15,6 +15,7 @@ #import "SentrySamplingContext.h" #import "SentryScope.h" #import "SentrySerialization.h" +#import "SentrySession+Private.h" #import "SentryTracer.h" #import "SentryTracesSampler.h" #import "SentryTransaction.h" @@ -33,6 +34,7 @@ @property (nonatomic, strong) id currentDateProvider; @property (nonatomic, strong) NSMutableArray> *installedIntegrations; @property (nonatomic, strong) NSMutableSet *installedIntegrationNames; +@property (nonatomic) NSUInteger errorsBeforeSession; @end @@ -53,6 +55,7 @@ - (instancetype)initWithClient:(nullable SentryClient *)client _installedIntegrationNames = [[NSMutableSet alloc] init]; _crashWrapper = [SentryCrashWrapper sharedInstance]; _tracesSampler = [[SentryTracesSampler alloc] initWithOptions:client.options]; + _errorsBeforeSession = 0; #if SENTRY_TARGET_PROFILING_SUPPORTED if (client.options.isProfilingEnabled) { _profilesSampler = [[SentryProfilesSampler alloc] initWithOptions:client.options]; @@ -93,6 +96,11 @@ - (void)startSession } _session = [[SentrySession alloc] initWithReleaseName:options.releaseName]; + if (_errorsBeforeSession > 0) { + _session.errors = _errorsBeforeSession; + _errorsBeforeSession = 0; + } + NSString *environment = options.environment; if (nil != environment) { _session.environment = environment; @@ -447,6 +455,7 @@ - (SentryId *)captureError:(NSError *)error withScope:(SentryScope *)scope withScope:scope incrementSessionErrors:^(void) { return [self incrementSessionErrors]; }]; } else { + _errorsBeforeSession++; return [client captureError:error withScope:scope]; } } @@ -468,6 +477,7 @@ - (SentryId *)captureException:(NSException *)exception withScope:(SentryScope * withScope:scope incrementSessionErrors:^(void) { return [self incrementSessionErrors]; }]; } else { + _errorsBeforeSession++; return [client captureException:exception withScope:scope]; } } diff --git a/Sources/Sentry/SentrySession.m b/Sources/Sentry/SentrySession.m index 8a65e80676..559dbccba6 100644 --- a/Sources/Sentry/SentrySession.m +++ b/Sources/Sentry/SentrySession.m @@ -1,8 +1,8 @@ -#import "SentrySession.h" #import "NSDate+SentryExtras.h" #import "SentryCurrentDate.h" #import "SentryInstallation.h" #import "SentryLog.h" +#import "SentrySession+Private.h" NS_ASSUME_NONNULL_BEGIN diff --git a/Sources/Sentry/include/SentrySession+Private.h b/Sources/Sentry/include/SentrySession+Private.h index 3a2b277ffa..1075b30261 100644 --- a/Sources/Sentry/include/SentrySession+Private.h +++ b/Sources/Sentry/include/SentrySession+Private.h @@ -6,7 +6,9 @@ NS_ASSUME_NONNULL_BEGIN NSString *nameForSentrySessionStatus(SentrySessionStatus status); @interface -SentrySession (Private) +SentrySession () + +@property (nonatomic) NSUInteger errors; - (void)setFlagInit; diff --git a/Tests/SentryTests/SentryHubTests.swift b/Tests/SentryTests/SentryHubTests.swift index 9f755822ef..2679cebfaf 100644 --- a/Tests/SentryTests/SentryHubTests.swift +++ b/Tests/SentryTests/SentryHubTests.swift @@ -394,6 +394,19 @@ class SentryHubTests: XCTestCase { XCTAssertEqual(1, fixture.client.captureSessionInvocations.count) } + func testCaptureErrorBeforeSession() { + let sut = fixture.getSut() + sut.capture(error: fixture.error, scope: fixture.scope).assertIsNotEmpty() + sut.startSession() + + XCTAssertEqual(fixture.client.captureErrorWithScopeInvocations.count, 1) + XCTAssertEqual(fixture.client.captureSessionInvocations.count, 1) + + if let session = fixture.client.captureSessionInvocations.first { + XCTAssertEqual(session.errors, 1) + } + } + func testCaptureWithoutIncreasingErrorCount() { let sut = fixture.getSut() sut.startSession()