Skip to content

Commit 474ae14

Browse files
fix: HTTP Client errors mark session as errored (#6633)
Automatically captured HTTP client errors now mark sessions as errored. Fixes GH-3742
1 parent 78a12dd commit 474ae14

File tree

12 files changed

+321
-256
lines changed

12 files changed

+321
-256
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Breaking Changes
6+
7+
- [HTTP Client errors](https://docs.sentry.io/platforms/apple/guides/ios/configuration/http-client-errors/) now mark sessions as errored (#6633)
8+
39
## 9.0.0-alpha.1
410

511
### Breaking Changes

SentryTestUtils/Sources/TestClient.swift

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,47 +66,46 @@ public class TestClient: SentryClientInternal {
6666
return SentryId()
6767
}
6868

69-
var captureErrorInvocations = Invocations<Error>()
69+
public var captureErrorInvocations = Invocations<Error>()
7070
public override func capture(error: Error) -> SentryId {
71+
super.capture(error: error)
72+
7173
captureErrorInvocations.record(error)
7274
return SentryId()
7375
}
7476

7577
public var captureErrorWithScopeInvocations = Invocations<(error: Error, scope: Scope)>()
7678
public override func capture(error: Error, scope: Scope) -> SentryId {
79+
super.capture(error: error, scope: scope)
80+
7781
captureErrorWithScopeInvocations.record((error, scope))
7882
return SentryId()
7983
}
8084

8185
var captureExceptionInvocations = Invocations<NSException>()
8286
public override func capture(exception: NSException) -> SentryId {
87+
super.capture(exception: exception)
88+
8389
captureExceptionInvocations.record(exception)
8490
return SentryId()
8591
}
8692

8793
public var captureExceptionWithScopeInvocations = Invocations<(exception: NSException, scope: Scope)>()
8894
public override func capture(exception: NSException, scope: Scope) -> SentryId {
95+
super.capture(exception: exception, scope: scope)
96+
8997
captureExceptionWithScopeInvocations.record((exception, scope))
9098
return SentryId()
9199
}
92100

93-
public var callSessionBlockWithIncrementSessionErrors = true
94-
@_spi(Private)
95-
public var captureErrorWithSessionInvocations = Invocations<(error: Error, session: SentrySession?, scope: Scope)>()
96-
@_spi(Private)
97-
public override func captureError(_ error: Error, with scope: Scope, incrementSessionErrors sessionBlock: @escaping () -> SentrySession) -> SentryId {
98-
captureErrorWithSessionInvocations.record((error, callSessionBlockWithIncrementSessionErrors ? sessionBlock() : nil, scope))
99-
return SentryId()
100-
}
101-
102-
@_spi(Private)
103-
public var captureExceptionWithSessionInvocations = Invocations<(exception: NSException, session: SentrySession?, scope: Scope)>()
104-
@_spi(Private)
105-
public override func capture(_ exception: NSException, with scope: Scope, incrementSessionErrors sessionBlock: @escaping () -> SentrySession) -> SentryId {
106-
captureExceptionWithSessionInvocations.record((exception, callSessionBlockWithIncrementSessionErrors ? sessionBlock() : nil, scope))
101+
@_spi(Private) public var captureEventIncrementingSessionErrorCountInvocations = Invocations<(event: Event, scope: Scope)>()
102+
@_spi(Private) public override func captureEventIncrementingSessionErrorCount(_ event: Event, with scope: Scope) -> SentryId {
103+
super.captureEventIncrementingSessionErrorCount(event, with: scope)
104+
105+
captureEventIncrementingSessionErrorCountInvocations.record((event, scope))
107106
return SentryId()
108107
}
109-
108+
110109
public var captureFatalEventInvocations = Invocations<(event: Event, scope: Scope)>()
111110
public override func captureFatalEvent(_ event: Event, with scope: Scope) -> SentryId {
112111
captureFatalEventInvocations.record((event, scope))

SentryTestUtils/Sources/TestHub.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public class TestHub: SentryHubInternal {
4444
return event.eventId
4545
}
4646

47+
@_spi(Private) public var capturedErrorEvents = Invocations<Event>()
48+
public override func captureErrorEvent(event: Event) -> SentryId {
49+
self.capturedErrorEvents.record((event))
50+
51+
return event.eventId
52+
}
53+
4754
public var capturedTransactionsWithScope = Invocations<(transaction: [String: Any], scope: Scope)>()
4855
public override func capture(_ transaction: Transaction, with scope: Scope) {
4956
capturedTransactionsWithScope.record((transaction.serialize(), scope))

Sources/Sentry/SentryClient.m

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,7 @@ - (SentryId *)captureException:(NSException *)exception
158158
- (SentryId *)captureException:(NSException *)exception withScope:(SentryScope *)scope
159159
{
160160
SentryEvent *event = [self buildExceptionEvent:exception];
161-
return [self sendEvent:event withScope:scope alwaysAttachStacktrace:YES];
162-
}
163-
164-
- (SentryId *)captureException:(NSException *)exception
165-
withScope:(SentryScope *)scope
166-
incrementSessionErrors:(SentrySession * (^)(void))sessionBlock
167-
{
168-
SentryEvent *event = [self buildExceptionEvent:exception];
169-
event = [self prepareEvent:event withScope:scope alwaysAttachStacktrace:YES];
170-
171-
if (event != nil) {
172-
SentrySession *session = sessionBlock();
173-
return [self sendEvent:event withSession:session withScope:scope];
174-
}
175-
176-
return SentryId.empty;
161+
return [self captureEventIncrementingSessionErrorCount:event withScope:scope];
177162
}
178163

179164
- (SentryEvent *)buildExceptionEvent:(NSException *)exception
@@ -204,22 +189,7 @@ - (SentryId *)captureError:(NSError *)error
204189
- (SentryId *)captureError:(NSError *)error withScope:(SentryScope *)scope
205190
{
206191
SentryEvent *event = [self buildErrorEvent:error];
207-
return [self sendEvent:event withScope:scope alwaysAttachStacktrace:YES];
208-
}
209-
210-
- (SentryId *)captureError:(NSError *)error
211-
withScope:(SentryScope *)scope
212-
incrementSessionErrors:(SentrySession * (^)(void))sessionBlock
213-
{
214-
SentryEvent *event = [self buildErrorEvent:error];
215-
event = [self prepareEvent:event withScope:scope alwaysAttachStacktrace:YES];
216-
217-
if (event != nil) {
218-
SentrySession *session = sessionBlock();
219-
return [self sendEvent:event withSession:session withScope:scope];
220-
}
221-
222-
return SentryId.empty;
192+
return [self captureEventIncrementingSessionErrorCount:event withScope:scope];
223193
}
224194

225195
- (SentryEvent *)buildErrorEvent:(NSError *)error
@@ -368,6 +338,26 @@ - (SentryId *)captureEvent:(SentryEvent *)event
368338
additionalEnvelopeItems:additionalEnvelopeItems];
369339
}
370340

341+
- (SentryId *)captureEventIncrementingSessionErrorCount:(SentryEvent *)event
342+
withScope:(SentryScope *)scope
343+
{
344+
SentryEvent *preparedEvent = [self prepareEvent:event
345+
withScope:scope
346+
alwaysAttachStacktrace:YES];
347+
348+
if (preparedEvent != nil) {
349+
SentrySession *session = nil;
350+
id<SentrySessionDelegate> delegate = self.sessionDelegate;
351+
if (delegate != nil) {
352+
session = [delegate incrementSessionErrors];
353+
}
354+
355+
return [self sendEvent:preparedEvent withSession:session withScope:scope];
356+
}
357+
358+
return SentryId.empty;
359+
}
360+
371361
- (SentryId *)sendEvent:(SentryEvent *)event
372362
withScope:(SentryScope *)scope
373363
alwaysAttachStacktrace:(BOOL)alwaysAttachStacktrace
@@ -445,7 +435,7 @@ - (SentryId *)sendEvent:(SentryEvent *)event
445435
}
446436

447437
- (SentryId *)sendEvent:(SentryEvent *)event
448-
withSession:(SentrySession *)session
438+
withSession:(nullable SentrySession *)session
449439
withScope:(SentryScope *)scope
450440
{
451441
if (event == nil) {
@@ -463,15 +453,22 @@ - (SentryId *)sendEvent:(SentryEvent *)event
463453

464454
SentryTraceContext *traceContext = [self getTraceStateWithEvent:event withScope:scope];
465455

466-
if (nil == session.releaseName || [session.releaseName length] == 0) {
456+
if (session == nil) {
457+
[self.transportAdapter sendEvent:event traceContext:traceContext attachments:attachments];
458+
return event.eventId;
459+
}
460+
461+
SentrySession *nonnullSession = SENTRY_UNWRAP_NULLABLE(SentrySession, session);
462+
463+
if (nonnullSession.releaseName == nil || [nonnullSession.releaseName length] == 0) {
467464
SENTRY_LOG_DEBUG(DropSessionLogMessage);
468465

469466
[self.transportAdapter sendEvent:event traceContext:traceContext attachments:attachments];
470467
return event.eventId;
471468
}
472469

473470
[self.transportAdapter sendEvent:event
474-
withSession:session
471+
withSession:nonnullSession
475472
traceContext:traceContext
476473
attachments:attachments];
477474

Sources/Sentry/SentryHub.m

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
NS_ASSUME_NONNULL_BEGIN
2727

28-
@interface SentryHubInternal () <SentryLoggerDelegate>
28+
@interface SentryHubInternal () <SentryLoggerDelegate, SentrySessionDelegate>
2929

3030
@property (nullable, atomic, strong) SentryClientInternal *client;
3131
@property (nullable, nonatomic, strong) SentryScope *scope;
@@ -69,6 +69,10 @@ - (instancetype)initWithClient:(nullable SentryClientInternal *)client
6969
_installedIntegrationNames = [[NSMutableSet alloc] init];
7070
_errorsBeforeSession = 0;
7171

72+
if (_client != nil) {
73+
_client.sessionDelegate = self;
74+
}
75+
7276
if (_scope) {
7377
[_crashWrapper enrichScope:SENTRY_UNWRAP_NULLABLE(SentryScope, _scope)];
7478
}
@@ -219,6 +223,8 @@ - (nullable SentrySession *)incrementSessionErrors
219223
[_session incrementErrors];
220224
[self storeCurrentSession:SENTRY_UNWRAP_NULLABLE(SentrySession, _session)];
221225
sessionCopy = [_session copy];
226+
} else {
227+
_errorsBeforeSession++;
222228
}
223229
}
224230

@@ -492,17 +498,10 @@ - (SentryId *)captureError:(NSError *)error
492498

493499
- (SentryId *)captureError:(NSError *)error withScope:(SentryScope *)scope
494500
{
495-
SentrySession *currentSession = _session;
496501
SentryClientInternal *client = self.client;
502+
497503
if (client != nil) {
498-
if (currentSession != nil) {
499-
return [client captureError:error
500-
withScope:scope
501-
incrementSessionErrors:^(void) { return [self incrementSessionErrors]; }];
502-
} else {
503-
_errorsBeforeSession++;
504-
return [client captureError:error withScope:scope];
505-
}
504+
return [client captureError:error withScope:scope];
506505
}
507506
return SentryId.empty;
508507
}
@@ -514,17 +513,21 @@ - (SentryId *)captureException:(NSException *)exception
514513

515514
- (SentryId *)captureException:(NSException *)exception withScope:(SentryScope *)scope
516515
{
517-
SentrySession *currentSession = _session;
518516
SentryClientInternal *client = self.client;
517+
519518
if (client != nil) {
520-
if (currentSession != nil) {
521-
return [client captureException:exception
522-
withScope:scope
523-
incrementSessionErrors:^(void) { return [self incrementSessionErrors]; }];
524-
} else {
525-
_errorsBeforeSession++;
526-
return [client captureException:exception withScope:scope];
527-
}
519+
return [client captureException:exception withScope:scope];
520+
}
521+
return SentryId.empty;
522+
}
523+
524+
- (SentryId *)captureErrorEvent:(SentryEvent *)event
525+
{
526+
SentryScope *scope = self.scope;
527+
SentryClientInternal *client = self.client;
528+
529+
if (client != nil) {
530+
return [client captureEventIncrementingSessionErrorCount:event withScope:scope];
528531
}
529532
return SentryId.empty;
530533
}
@@ -575,7 +578,13 @@ - (nullable SentryClientInternal *)getClient
575578

576579
- (void)bindClient:(nullable SentryClientInternal *)client
577580
{
581+
self.client.sessionDelegate = nil;
582+
578583
self.client = client;
584+
585+
if (client != nil) {
586+
client.sessionDelegate = self;
587+
}
579588
}
580589

581590
- (SentryScope *)scope

Sources/Sentry/SentryNetworkTracker.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ - (void)captureFailedRequests:(NSURLSessionTask *)sessionTask
393393

394394
event.context = context;
395395

396-
[SentrySDK captureEvent:event];
396+
[SentrySDKInternal.currentHub captureErrorEvent:event];
397397
}
398398

399399
- (BOOL)containsStatusCode:(NSInteger)statusCode

Sources/Sentry/include/SentryClient+Private.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
@class SentrySession;
1313
@class SentryDefaultThreadInspector;
1414

15+
@protocol SentrySessionDelegate <NSObject>
16+
17+
- (nullable SentrySession *)incrementSessionErrors;
18+
19+
@end
20+
1521
NS_ASSUME_NONNULL_BEGIN
1622

1723
@protocol SentryClientAttachmentProcessor <NSObject>
@@ -27,14 +33,7 @@ NS_ASSUME_NONNULL_BEGIN
2733
NSMutableArray<id<SentryClientAttachmentProcessor>> *attachmentProcessors;
2834
@property (nonatomic, strong) SentryDefaultThreadInspector *threadInspector;
2935
@property (nonatomic, strong) SentryFileManager *fileManager;
30-
31-
- (SentryId *)captureError:(NSError *)error
32-
withScope:(SentryScope *)scope
33-
incrementSessionErrors:(SentrySession * (^)(void))sessionBlock;
34-
35-
- (SentryId *)captureException:(NSException *)exception
36-
withScope:(SentryScope *)scope
37-
incrementSessionErrors:(SentrySession * (^)(void))sessionBlock;
36+
@property (nonatomic, weak, nullable) id<SentrySessionDelegate> sessionDelegate;
3837

3938
- (SentryId *)captureFatalEvent:(SentryEvent *)event withScope:(SentryScope *)scope;
4039

@@ -56,6 +55,9 @@ NS_ASSUME_NONNULL_BEGIN
5655
additionalEnvelopeItems:(NSArray<SentryEnvelopeItem *> *)additionalEnvelopeItems
5756
NS_SWIFT_NAME(capture(event:scope:additionalEnvelopeItems:));
5857

58+
- (SentryId *)captureEventIncrementingSessionErrorCount:(SentryEvent *)event
59+
withScope:(SentryScope *)scope;
60+
5961
- (void)captureReplayEvent:(SentryReplayEvent *)replayEvent
6062
replayRecording:(SentryReplayRecording *)replayRecording
6163
video:(NSURL *)videoURL

Sources/Sentry/include/SentryHub+Private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ NS_ASSUME_NONNULL_BEGIN
6666
additionalEnvelopeItems:(NSArray<SentryEnvelopeItem *> *)additionalEnvelopeItems
6767
NS_SWIFT_NAME(capture(event:scope:additionalEnvelopeItems:));
6868

69+
- (SentryId *)captureErrorEvent:(SentryEvent *)event NS_SWIFT_NAME(captureErrorEvent(event:));
70+
6971
- (void)captureSerializedFeedback:(NSDictionary *)serializedFeedback
7072
withEventId:(NSString *)feedbackEventId
7173
attachments:(NSArray<SentryAttachment *> *)feedbackAttachments;

0 commit comments

Comments
 (0)