Skip to content

feat: Mark exceptions as fatal for macOS #5497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
10 changes: 9 additions & 1 deletion Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,22 @@

event.exceptions = @[ sentryException ];

SentryMechanism *mechanism = [[SentryMechanism alloc] initWithType:@"NSException"];
mechanism.desc = exception.reason;
mechanism.data = sentry_sanitize(exception.userInfo);

Check warning on line 220 in Sources/Sentry/SentryClient.m

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentryClient.m#L218-L220

Added lines #L218 - L220 were not covered by tests

#if TARGET_OS_OSX
// When a exception class is SentryUseNSExceptionCallstackWrapper, we should use the thread from
// it
if ([exception isKindOfClass:[SentryUseNSExceptionCallstackWrapper class]]) {
event.threads = [(SentryUseNSExceptionCallstackWrapper *)exception buildThreads];
event.level = kSentryLevelFatal;

// Only exceptions through `_crashOnExceptions` are guaranteed to be unhandled
mechanism.handled = @(NO);
}
#endif

sentryException.mechanism = mechanism;

Check warning on line 233 in Sources/Sentry/SentryClient.m

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentryClient.m#L233

Added line #L233 was not covered by tests
[self setUserInfo:exception.userInfo withEvent:event];
return event;
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/Sentry/SentryCrashExceptionApplicationHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# import "SentryDependencyContainer.h"
# import "SentrySDK+Private.h"
# import "SentrySDK.h"
# import "SentryScope.h"
# import "SentrySwift.h"

@implementation SentryCrashExceptionApplicationHelper

Expand All @@ -21,9 +23,11 @@ + (void)reportException:(NSException *)exception

+ (void)_crashOnException:(NSException *)exception
{
[SentrySDK captureCrashOnException:exception];
SentryScope *scope = [[SentryScope alloc] initWithScope:SentrySDK.currentHub.scope];
[scope setLevel:kSentryLevelFatal];
[SentrySDK captureCrashOnException:exception withScope:scope];
# if !(SENTRY_TEST || SENTRY_TEST_CI)
abort();
// abort();
# endif
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/SentrySDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,15 @@ + (SentryId *)captureException:(NSException *)exception withScope:(SentryScope *

#if TARGET_OS_OSX

+ (SentryId *)captureCrashOnException:(NSException *)exception
+ (SentryId *)captureCrashOnException:(NSException *)exception withScope:(SentryScope *)scope
{
SentryUseNSExceptionCallstackWrapper *wrappedException =
[[SentryUseNSExceptionCallstackWrapper alloc]
initWithName:exception.name
reason:exception.reason
userInfo:exception.userInfo
callStackReturnAddresses:exception.callStackReturnAddresses];
return [SentrySDK captureException:wrappedException withScope:SentrySDK.currentHub.scope];
return [SentrySDK captureException:wrappedException withScope:scope];
}

#endif // TARGET_OS_OSX
Expand Down
3 changes: 2 additions & 1 deletion Sources/Sentry/include/SentrySDK+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ NS_ASSUME_NONNULL_BEGIN
*
*/
+ (SentryId *)captureCrashOnException:(NSException *)exception
NS_SWIFT_NAME(captureCrashOn(exception:));
withScope:(SentryScope *)scope
NS_SWIFT_NAME(captureCrashOn(exception:withScope:));

#endif // TARGET_OS_OSX

Expand Down
2 changes: 1 addition & 1 deletion Tests/SentryTests/SentrySDKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ class SentrySDKTests: XCTestCase {
func testCaptureCrashOnException() {
givenSdkWithHub()

SentrySDK.captureCrashOn(exception: fixture.exception)
SentrySDK.captureCrashOn(exception: fixture.exception, withScope: fixture.scope)

let client = fixture.client
XCTAssertEqual(1, client.captureExceptionWithScopeInvocations.count)
Expand Down
Loading