Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Breaking Changes

- [HTTP Client errors](https://docs.sentry.io/platforms/apple/guides/ios/configuration/http-client-errors/) now mark sessions as errored (#6633)

## 9.0.0-alpha.1

### Breaking Changes
Expand Down
31 changes: 15 additions & 16 deletions SentryTestUtils/Sources/TestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,47 +66,46 @@ public class TestClient: SentryClientInternal {
return SentryId()
}

var captureErrorInvocations = Invocations<Error>()
public var captureErrorInvocations = Invocations<Error>()
public override func capture(error: Error) -> SentryId {
super.capture(error: error)

captureErrorInvocations.record(error)
return SentryId()
}

public var captureErrorWithScopeInvocations = Invocations<(error: Error, scope: Scope)>()
public override func capture(error: Error, scope: Scope) -> SentryId {
super.capture(error: error, scope: scope)

captureErrorWithScopeInvocations.record((error, scope))
return SentryId()
}

var captureExceptionInvocations = Invocations<NSException>()
public override func capture(exception: NSException) -> SentryId {
super.capture(exception: exception)

captureExceptionInvocations.record(exception)
return SentryId()
}

public var captureExceptionWithScopeInvocations = Invocations<(exception: NSException, scope: Scope)>()
public override func capture(exception: NSException, scope: Scope) -> SentryId {
super.capture(exception: exception, scope: scope)

captureExceptionWithScopeInvocations.record((exception, scope))
return SentryId()
}

public var callSessionBlockWithIncrementSessionErrors = true
@_spi(Private)
public var captureErrorWithSessionInvocations = Invocations<(error: Error, session: SentrySession?, scope: Scope)>()
@_spi(Private)
public override func captureError(_ error: Error, with scope: Scope, incrementSessionErrors sessionBlock: @escaping () -> SentrySession) -> SentryId {
captureErrorWithSessionInvocations.record((error, callSessionBlockWithIncrementSessionErrors ? sessionBlock() : nil, scope))
return SentryId()
}

@_spi(Private)
public var captureExceptionWithSessionInvocations = Invocations<(exception: NSException, session: SentrySession?, scope: Scope)>()
@_spi(Private)
public override func capture(_ exception: NSException, with scope: Scope, incrementSessionErrors sessionBlock: @escaping () -> SentrySession) -> SentryId {
captureExceptionWithSessionInvocations.record((exception, callSessionBlockWithIncrementSessionErrors ? sessionBlock() : nil, scope))
@_spi(Private) public var captureEventIncrementingSessionErrorCountInvocations = Invocations<(event: Event, scope: Scope)>()
@_spi(Private) public override func captureEventIncrementingSessionErrorCount(_ event: Event, with scope: Scope) -> SentryId {
super.captureEventIncrementingSessionErrorCount(event, with: scope)

captureEventIncrementingSessionErrorCountInvocations.record((event, scope))
return SentryId()
}

public var captureFatalEventInvocations = Invocations<(event: Event, scope: Scope)>()
public override func captureFatalEvent(_ event: Event, with scope: Scope) -> SentryId {
captureFatalEventInvocations.record((event, scope))
Expand Down
7 changes: 7 additions & 0 deletions SentryTestUtils/Sources/TestHub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public class TestHub: SentryHubInternal {
return event.eventId
}

@_spi(Private) public var capturedErrorEvents = Invocations<Event>()
public override func captureErrorEvent(event: Event) -> SentryId {
self.capturedErrorEvents.record((event))

return event.eventId
}

public var capturedTransactionsWithScope = Invocations<(transaction: [String: Any], scope: Scope)>()
public override func capture(_ transaction: Transaction, with scope: Scope) {
capturedTransactionsWithScope.record((transaction.serialize(), scope))
Expand Down
67 changes: 32 additions & 35 deletions Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,7 @@ - (SentryId *)captureException:(NSException *)exception
- (SentryId *)captureException:(NSException *)exception withScope:(SentryScope *)scope
{
SentryEvent *event = [self buildExceptionEvent:exception];
return [self sendEvent:event withScope:scope alwaysAttachStacktrace:YES];
}

- (SentryId *)captureException:(NSException *)exception
withScope:(SentryScope *)scope
incrementSessionErrors:(SentrySession * (^)(void))sessionBlock
{
SentryEvent *event = [self buildExceptionEvent:exception];
event = [self prepareEvent:event withScope:scope alwaysAttachStacktrace:YES];

if (event != nil) {
SentrySession *session = sessionBlock();
return [self sendEvent:event withSession:session withScope:scope];
}

return SentryId.empty;
return [self captureEventIncrementingSessionErrorCount:event withScope:scope];
}

- (SentryEvent *)buildExceptionEvent:(NSException *)exception
Expand Down Expand Up @@ -204,22 +189,7 @@ - (SentryId *)captureError:(NSError *)error
- (SentryId *)captureError:(NSError *)error withScope:(SentryScope *)scope
{
SentryEvent *event = [self buildErrorEvent:error];
return [self sendEvent:event withScope:scope alwaysAttachStacktrace:YES];
}

- (SentryId *)captureError:(NSError *)error
withScope:(SentryScope *)scope
incrementSessionErrors:(SentrySession * (^)(void))sessionBlock
{
SentryEvent *event = [self buildErrorEvent:error];
event = [self prepareEvent:event withScope:scope alwaysAttachStacktrace:YES];

if (event != nil) {
SentrySession *session = sessionBlock();
return [self sendEvent:event withSession:session withScope:scope];
}

return SentryId.empty;
return [self captureEventIncrementingSessionErrorCount:event withScope:scope];
}

- (SentryEvent *)buildErrorEvent:(NSError *)error
Expand Down Expand Up @@ -368,6 +338,26 @@ - (SentryId *)captureEvent:(SentryEvent *)event
additionalEnvelopeItems:additionalEnvelopeItems];
}

- (SentryId *)captureEventIncrementingSessionErrorCount:(SentryEvent *)event
withScope:(SentryScope *)scope
{
SentryEvent *preparedEvent = [self prepareEvent:event
withScope:scope
alwaysAttachStacktrace:YES];

if (preparedEvent != nil) {
SentrySession *session = nil;
id<SentrySessionDelegate> delegate = self.sessionDelegate;
if (delegate != nil) {
session = [delegate incrementSessionErrors];
}

return [self sendEvent:preparedEvent withSession:session withScope:scope];
}

return SentryId.empty;
}

- (SentryId *)sendEvent:(SentryEvent *)event
withScope:(SentryScope *)scope
alwaysAttachStacktrace:(BOOL)alwaysAttachStacktrace
Expand Down Expand Up @@ -445,7 +435,7 @@ - (SentryId *)sendEvent:(SentryEvent *)event
}

- (SentryId *)sendEvent:(SentryEvent *)event
withSession:(SentrySession *)session
withSession:(nullable SentrySession *)session
withScope:(SentryScope *)scope
{
if (event == nil) {
Expand All @@ -463,15 +453,22 @@ - (SentryId *)sendEvent:(SentryEvent *)event

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

if (nil == session.releaseName || [session.releaseName length] == 0) {
if (session == nil) {
[self.transportAdapter sendEvent:event traceContext:traceContext attachments:attachments];
return event.eventId;
}

SentrySession *nonnullSession = SENTRY_UNWRAP_NULLABLE(SentrySession, session);

if (nonnullSession.releaseName == nil || [nonnullSession.releaseName length] == 0) {
SENTRY_LOG_DEBUG(DropSessionLogMessage);

[self.transportAdapter sendEvent:event traceContext:traceContext attachments:attachments];
return event.eventId;
}

[self.transportAdapter sendEvent:event
withSession:session
withSession:nonnullSession
traceContext:traceContext
attachments:attachments];

Expand Down
47 changes: 28 additions & 19 deletions Sources/Sentry/SentryHub.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

NS_ASSUME_NONNULL_BEGIN

@interface SentryHubInternal () <SentryLoggerDelegate>
@interface SentryHubInternal () <SentryLoggerDelegate, SentrySessionDelegate>

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

if (_client != nil) {
_client.sessionDelegate = self;
}

if (_scope) {
[_crashWrapper enrichScope:SENTRY_UNWRAP_NULLABLE(SentryScope, _scope)];
}
Expand Down Expand Up @@ -219,6 +223,8 @@ - (nullable SentrySession *)incrementSessionErrors
[_session incrementErrors];
[self storeCurrentSession:SENTRY_UNWRAP_NULLABLE(SentrySession, _session)];
sessionCopy = [_session copy];
} else {
_errorsBeforeSession++;
}
}

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

- (SentryId *)captureError:(NSError *)error withScope:(SentryScope *)scope
{
SentrySession *currentSession = _session;
SentryClientInternal *client = self.client;

if (client != nil) {
if (currentSession != nil) {
return [client captureError:error
withScope:scope
incrementSessionErrors:^(void) { return [self incrementSessionErrors]; }];
} else {
_errorsBeforeSession++;
return [client captureError:error withScope:scope];
}
return [client captureError:error withScope:scope];
}
return SentryId.empty;
}
Expand All @@ -514,17 +513,21 @@ - (SentryId *)captureException:(NSException *)exception

- (SentryId *)captureException:(NSException *)exception withScope:(SentryScope *)scope
{
SentrySession *currentSession = _session;
SentryClientInternal *client = self.client;

if (client != nil) {
if (currentSession != nil) {
return [client captureException:exception
withScope:scope
incrementSessionErrors:^(void) { return [self incrementSessionErrors]; }];
} else {
_errorsBeforeSession++;
return [client captureException:exception withScope:scope];
}
return [client captureException:exception withScope:scope];
}
return SentryId.empty;
}

- (SentryId *)captureErrorEvent:(SentryEvent *)event
{
SentryScope *scope = self.scope;
SentryClientInternal *client = self.client;

if (client != nil) {
return [client captureEventIncrementingSessionErrorCount:event withScope:scope];
}
return SentryId.empty;
}
Expand Down Expand Up @@ -575,7 +578,13 @@ - (nullable SentryClientInternal *)getClient

- (void)bindClient:(nullable SentryClientInternal *)client
{
self.client.sessionDelegate = nil;

self.client = client;

if (client != nil) {
client.sessionDelegate = self;
}
}

- (SentryScope *)scope
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryNetworkTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ - (void)captureFailedRequests:(NSURLSessionTask *)sessionTask

event.context = context;

[SentrySDK captureEvent:event];
[SentrySDKInternal.currentHub captureErrorEvent:event];
}

- (BOOL)containsStatusCode:(NSInteger)statusCode
Expand Down
18 changes: 10 additions & 8 deletions Sources/Sentry/include/SentryClient+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
@class SentrySession;
@class SentryDefaultThreadInspector;

@protocol SentrySessionDelegate <NSObject>

- (nullable SentrySession *)incrementSessionErrors;

@end

NS_ASSUME_NONNULL_BEGIN

@protocol SentryClientAttachmentProcessor <NSObject>
Expand All @@ -27,14 +33,7 @@ NS_ASSUME_NONNULL_BEGIN
NSMutableArray<id<SentryClientAttachmentProcessor>> *attachmentProcessors;
@property (nonatomic, strong) SentryDefaultThreadInspector *threadInspector;
@property (nonatomic, strong) SentryFileManager *fileManager;

- (SentryId *)captureError:(NSError *)error
withScope:(SentryScope *)scope
incrementSessionErrors:(SentrySession * (^)(void))sessionBlock;

- (SentryId *)captureException:(NSException *)exception
withScope:(SentryScope *)scope
incrementSessionErrors:(SentrySession * (^)(void))sessionBlock;
@property (nonatomic, weak, nullable) id<SentrySessionDelegate> sessionDelegate;

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

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

- (SentryId *)captureEventIncrementingSessionErrorCount:(SentryEvent *)event
withScope:(SentryScope *)scope;

- (void)captureReplayEvent:(SentryReplayEvent *)replayEvent
replayRecording:(SentryReplayRecording *)replayRecording
video:(NSURL *)videoURL
Expand Down
2 changes: 2 additions & 0 deletions Sources/Sentry/include/SentryHub+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ NS_ASSUME_NONNULL_BEGIN
additionalEnvelopeItems:(NSArray<SentryEnvelopeItem *> *)additionalEnvelopeItems
NS_SWIFT_NAME(capture(event:scope:additionalEnvelopeItems:));

- (SentryId *)captureErrorEvent:(SentryEvent *)event NS_SWIFT_NAME(captureErrorEvent(event:));

- (void)captureSerializedFeedback:(NSDictionary *)serializedFeedback
withEventId:(NSString *)feedbackEventId
attachments:(NSArray<SentryAttachment *> *)feedbackAttachments;
Expand Down
Loading
Loading