diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f156ed2ced..612f1f181ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## unreleased +- feat: Use error domain and code for event message #750 - feat: Replace passing nullable Scope with overloads #743 !Breaking - feat: Remove SDK frames when attaching stacktrace #739 - fix: captureException crates a event type=error #746 diff --git a/Samples/iOS-ObjectiveC/iOS-ObjectiveC/ViewController.m b/Samples/iOS-ObjectiveC/iOS-ObjectiveC/ViewController.m index 615e83e1a32..e4b5f4d51c2 100644 --- a/Samples/iOS-ObjectiveC/iOS-ObjectiveC/ViewController.m +++ b/Samples/iOS-ObjectiveC/iOS-ObjectiveC/ViewController.m @@ -45,7 +45,7 @@ - (IBAction)captureMessage:(id)sender - (IBAction)captureError:(id)sender { NSError *error = - [[NSError alloc] initWithDomain:@"" + [[NSError alloc] initWithDomain:@"SampleErrorDomain" code:0 userInfo:@{ NSLocalizedDescriptionKey : @"Object does not exist" }]; [SentrySDK captureError:error diff --git a/Samples/iOS-Swift/iOS-Swift/ViewController.swift b/Samples/iOS-Swift/iOS-Swift/ViewController.swift index ff1171913b0..219442258ae 100644 --- a/Samples/iOS-Swift/iOS-Swift/ViewController.swift +++ b/Samples/iOS-Swift/iOS-Swift/ViewController.swift @@ -35,7 +35,8 @@ class ViewController: UIViewController { } @IBAction func captureError(_ sender: Any) { - let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Object does not exist"]) + let error = NSError(domain: "SampleErrorDomain", code: 1, userInfo: [NSLocalizedDescriptionKey: "Object does not exist"]) + SentrySDK.capture(error: error) { (scope) in // Changes in here will only be captured for this event // The scope in this callback is a clone of the current scope diff --git a/Samples/tvOS-Swift/tvOS-Swift/ContentView.swift b/Samples/tvOS-Swift/tvOS-Swift/ContentView.swift index 6b9c63ef827..5bdf70ee725 100644 --- a/Samples/tvOS-Swift/tvOS-Swift/ContentView.swift +++ b/Samples/tvOS-Swift/tvOS-Swift/ContentView.swift @@ -14,7 +14,7 @@ struct ContentView: View { } var captureErrorAction: () -> Void = { - let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Object does not exist"]) + let error = NSError(domain: "SampleErrorDomain", code: 1, userInfo: [NSLocalizedDescriptionKey: "Object does not exist"]) SentrySDK.capture(error: error) { (scope) in scope.setTag(value: "value", key: "myTag") } diff --git a/Samples/watchOS-Swift/watchOS-Swift WatchKit Extension/ContentView.swift b/Samples/watchOS-Swift/watchOS-Swift WatchKit Extension/ContentView.swift index 78eac01e53c..bc18892303b 100644 --- a/Samples/watchOS-Swift/watchOS-Swift WatchKit Extension/ContentView.swift +++ b/Samples/watchOS-Swift/watchOS-Swift WatchKit Extension/ContentView.swift @@ -14,7 +14,7 @@ struct ContentView: View { } var captureErrorAction: () -> Void = { - let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Object does not exist"]) + let error = NSError(domain: "SampleErrorDomain", code: 1, userInfo: [NSLocalizedDescriptionKey: "Object does not exist"]) SentrySDK.capture(error: error) { (scope) in scope.setTag(value: "value", key: "myTag") } diff --git a/Sources/Sentry/SentryClient.m b/Sources/Sentry/SentryClient.m index c0dbe8b81cb..5fb323614ce 100644 --- a/Sources/Sentry/SentryClient.m +++ b/Sources/Sentry/SentryClient.m @@ -159,7 +159,7 @@ - (SentryId *)captureError:(NSError *)error - (SentryEvent *)buildErrorEvent:(NSError *)error { SentryEvent *event = [[SentryEvent alloc] initWithLevel:kSentryLevelError]; - event.message = error.localizedDescription; + event.message = [NSString stringWithFormat:@"%@ %ld", error.domain, (long)error.code]; [self setUserInfo:error.userInfo withEvent:event]; return event; } diff --git a/Tests/SentryTests/SentryClientTests.swift b/Tests/SentryTests/SentryClientTests.swift index 5580e244e01..5d7b134a3b3 100644 --- a/Tests/SentryTests/SentryClientTests.swift +++ b/Tests/SentryTests/SentryClientTests.swift @@ -60,7 +60,7 @@ class SentryClientTest: XCTestCase { } } - private let error = NSError(domain: "domain", code: 0, userInfo: [NSLocalizedDescriptionKey: "Object does not exist"]) + private let error = NSError(domain: "domain", code: -20, userInfo: [NSLocalizedDescriptionKey: "Object does not exist"]) private let exception = NSException(name: NSExceptionName("My Custom exception"), reason: "User clicked the button", userInfo: nil) @@ -213,6 +213,16 @@ class SentryClientTest: XCTestCase { } } + func testCaptureErrorWithEnum() { + let eventId = fixture.getSut().capture(error: TestError.invalidTest) + + eventId.assertIsNotEmpty() + let error = TestError.invalidTest as NSError + assertLastSentEvent { actual in + XCTAssertEqual("\(error.domain) \(error.code)", actual.message) + } + } + func testCaptureErrorWithSession() { let eventId = fixture.getSut().captureError(error, with: fixture.session, with: Scope()) @@ -495,7 +505,7 @@ class SentryClientTest: XCTestCase { private func assertValidErrorEvent(_ event: Event) { XCTAssertEqual(SentryLevel.error, event.level) - XCTAssertEqual(error.localizedDescription, event.message) + XCTAssertEqual("\(error.domain) \(error.code)", event.message) assertValidDebugMeta(actual: event.debugMeta) assertValidThreads(actual: event.threads) } @@ -554,4 +564,10 @@ class SentryClientTest: XCTestCase { XCTAssertEqual(0, fixture.transport.sentEventsWithSession.count) XCTAssertEqual(0, fixture.transport.sentEvents.count) } + + private enum TestError : Error { + case invalidTest + case testIsFailing + case somethingElse + } }