Skip to content

Commit

Permalink
feat: Add error to SentryEvent (#944)
Browse files Browse the repository at this point in the history
Add an initializer initWithError and property error to SentryEvent. This change gives
our users the possibility to access the error in beforeSend.
  • Loading branch information
philipphofmann authored Feb 11, 2021
1 parent f9a2d4a commit 9e67662
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## unreleased

- feat: Add error to SentryEvent #944
- fix: Mark SentryEvent.message as Nullable #943
- fix: Stacktrace inApp marking on Simulators #942
- feat: Group NSError by domain and code #941
Expand Down
16 changes: 16 additions & 0 deletions Sources/Sentry/Public/SentryEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ NS_SWIFT_NAME(Event)
*/
@property (nonatomic, strong) SentryMessage *_Nullable message;

/**
* The error of the event. This property adds convenience to access the error directly in
* beforeSend. This property is not serialized. Instead when preparing the event the SentryClient
* puts the error into exceptions.
*/
@property (nonatomic, copy) NSError *_Nullable error;

/**
* NSDate of when the event occured
*/
Expand Down Expand Up @@ -164,6 +171,15 @@ NS_SWIFT_NAME(Event)
*/
- (instancetype)initWithLevel:(enum SentryLevel)level NS_DESIGNATED_INITIALIZER;

/**
* Initializes a SentryEvent with an NSError and sets the level to SentryLevelError.
*
* @param error The error of the event.
*
* @return The initialized SentryEvent.
*/
- (instancetype)initWithError:(NSError *)error;

@end

NS_ASSUME_NONNULL_END
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ - (SentryId *)captureError:(NSError *)error

- (SentryEvent *)buildErrorEvent:(NSError *)error
{
SentryEvent *event = [[SentryEvent alloc] initWithLevel:kSentryLevelError];
SentryEvent *event = [[SentryEvent alloc] initWithError:error];
NSString *errorCodeAsString = [NSString stringWithFormat:@"%ld", (long)error.code];
SentryException *sentryException = [[SentryException alloc] initWithValue:errorCodeAsString
type:error.domain];
Expand Down
7 changes: 7 additions & 0 deletions Sources/Sentry/SentryEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ - (instancetype)initWithLevel:(enum SentryLevel)level
return self;
}

- (instancetype)initWithError:(NSError *)error
{
self = [self initWithLevel:kSentryLevelError];
self.error = error;
return self;
}

- (NSDictionary<NSString *, id> *)serialize
{
if (nil == self.timestamp) {
Expand Down
8 changes: 7 additions & 1 deletion Tests/SentryTests/Protocol/SentryEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,17 @@ class SentryEventTests: XCTestCase {
XCTAssertNil(actual["breadcrumbs"])
}

func testInitWithError() {
let error = CocoaError(CocoaError.coderInvalidValue)
let event = Event(error: error)
XCTAssertEqual(error, event.error as? CocoaError)
}

func testSerializeWithoutMessage() {
let actual = Event().serialize()
XCTAssertNil(actual["message"])
}

func testMessageIsNil() {
XCTAssertNil(Event().message)
}
Expand Down
1 change: 1 addition & 0 deletions Tests/SentryTests/SentryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ class SentryClientTest: XCTestCase {

private func assertValidErrorEvent(_ event: Event, _ error: NSError) {
XCTAssertEqual(SentryLevel.error, event.level)
XCTAssertEqual(error, event.error as NSError?)

guard let exceptions = event.exceptions else {
XCTFail("Event should contain one exception"); return
Expand Down

0 comments on commit 9e67662

Please sign in to comment.