Skip to content
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

feat: Add onCrashedLastRun #808

Merged
merged 21 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0961c89
feat: Add onCrashedLastRun
philipphofmann Oct 21, 2020
416321d
Committing formatted code
Oct 21, 2020
bd556f4
Merge branch 'master' into feat/on-crashed-last-run
philipphofmann Nov 12, 2020
fd0d836
Call callback only for first crash event
philipphofmann Nov 12, 2020
eb52611
Add comment about downside of callback
philipphofmann Nov 12, 2020
29ce980
Merge branch 'master' into feat/on-crashed-last-run
philipphofmann Nov 12, 2020
cebee03
Merge branch 'master' into feat/on-crashed-last-run
philipphofmann Nov 16, 2020
16ff083
Check if event contains unhandled exception
philipphofmann Nov 16, 2020
c99eeab
Committing formatted code
Nov 16, 2020
cc4c291
Merge branch 'master' into feat/on-crashed-last-run
philipphofmann Nov 16, 2020
1633157
Merge branch 'master' into feat/on-crashed-last-run
philipphofmann Nov 18, 2020
dacc561
Add tests
philipphofmann Nov 19, 2020
b3e49d8
Undo changes in iOS-Swift AppDelegate
philipphofmann Nov 19, 2020
80439ea
Remove SentryId from SentryDefines
philipphofmann Nov 19, 2020
fa66553
Update changelog
philipphofmann Nov 19, 2020
44b4f5e
Merge branch 'master' into feat/on-crashed-last-run
philipphofmann Nov 20, 2020
01ee8fc
Merge branch 'master' into feat/on-crashed-last-run
philipphofmann Nov 23, 2020
8a9faea
Toggle BOOL after action for callOnCrashedLastRun
philipphofmann Nov 24, 2020
b04e388
Fix typos
philipphofmann Nov 24, 2020
4950e6b
Add captureCrash to SentryClient
philipphofmann Nov 24, 2020
008008d
Merge branch 'master' into feat/on-crashed-last-run
philipphofmann Nov 25, 2020
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
14 changes: 14 additions & 0 deletions Samples/iOS-Swift/iOS-Swift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

SentrySDK.start { options in
options.dsn = "https://387714a4f3654858a6f0ff63fd551485@o447951.ingest.sentry.io/5428557"

options.beforeSend = { event in
return event
}

options.onCrashedLastRun = { event in
self.displayUserFeedback(event: event)
}

options.debug = true
options.logLevel = SentryLogLevel.verbose
options.attachStacktrace = true
Expand All @@ -19,6 +25,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

return true
}

private func displayUserFeedback(event: Event) {
let userFeedback = UserFeedback(eventId: event.eventId)
userFeedback.comments = "It broke on iOS-Swift from onCrashedLastRun."
userFeedback.email = "john@me.com"
userFeedback.name = "John Me"
SentrySDK.capture(userFeedback: userFeedback)
}

// MARK: UISceneSession Lifecycle

Expand Down
7 changes: 6 additions & 1 deletion Sources/Sentry/Public/SentryDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
-(instancetype)init NS_UNAVAILABLE; \
+(instancetype) new NS_UNAVAILABLE;

@class SentryEvent, SentryBreadcrumb;
@class SentryEvent, SentryBreadcrumb, SentryId;

/**
* Block used for returning after a request finished
Expand All @@ -48,6 +48,11 @@ typedef SentryBreadcrumb *_Nullable (^SentryBeforeBreadcrumbCallback)(
*/
typedef SentryEvent *_Nullable (^SentryBeforeSendEventCallback)(SentryEvent *_Nonnull event);

/**
* A callback to be notified when the last program execution terminated with a crash.
*/
typedef void (^SentryOnCrashedLastRunCallback)(SentryEvent *_Nonnull event);

/**
* Block can be used to determine if an event should be queued and stored
* locally. It will be tried to send again after next successful send. Note that
Expand Down
10 changes: 10 additions & 0 deletions Sources/Sentry/Public/SentryOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ NS_SWIFT_NAME(Options)
*/
@property (nonatomic, copy) SentryBeforeBreadcrumbCallback _Nullable beforeBreadcrumb;

/**
* This gets called shortly after the initialization of the SDK when the last program execution
* terminated with a crash.
*
* @discussion When the SDK has to send multiple crash events, which can happen when the program
* terminates with a crash before the SDK can send the crash event, this callback is only executed
* for the first crash event. You can look into beforeSend if you prefer a callback for every event.
*/
@property (nonatomic, copy) SentryOnCrashedLastRunCallback _Nullable onCrashedLastRun;

/**
* Array of integrations to install.
*/
Expand Down
13 changes: 13 additions & 0 deletions Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#import "SentryMessage.h"
#import "SentryMeta.h"
#import "SentryOptions.h"
#import "SentrySDK+Private.h"
#import "SentrySDK.h"
#import "SentryScope.h"
#import "SentryStacktraceBuilder.h"
#import "SentryThreadInspector.h"
Expand Down Expand Up @@ -366,6 +368,17 @@ - (SentryEvent *_Nullable)prepareEvent:(SentryEvent *)event
event = self.options.beforeSend(event);
}

// The callback is also called when SentrySDK.crashedLastRun is true and the
// SentryCrashReportSink didn't capture the crash event yet and the user manually captures an
// event with level fatal.
// We could also call this callback in SentryHub.captureCrashEvent, but then the event would
// miss detail from SentryClient.prepareEvent.
if (nil != self.options.onCrashedLastRun && SentrySDK.crashedLastRun
&& !SentrySDK.crashedLastRunCalled && event.level == kSentryLevelFatal) {
SentrySDK.crashedLastRunCalled = YES;
self.options.onCrashedLastRun(event);
}

return event;
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/Sentry/SentryOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ - (void)validateOptions:(NSDictionary<NSString *, id> *)options
self.beforeBreadcrumb = options[@"beforeBreadcrumb"];
}

if (nil != options[@"onCrashedLastRun"]) {
self.onCrashedLastRun = options[@"onCrashedLastRun"];
}

if (nil != options[@"integrations"]) {
self.integrations = options[@"integrations"];
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Sentry/include/SentrySDK+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ NS_ASSUME_NONNULL_BEGIN

+ (void)captureCrashEvent:(SentryEvent *)event;

@property (nonatomic, class) BOOL crashedLastRunCalled;

@end

NS_ASSUME_NONNULL_END