From c1846ca7682c0c7eb6870ff9d951834fafaeca7b Mon Sep 17 00:00:00 2001 From: Geoff Pado Date: Fri, 2 Aug 2024 20:35:46 -0700 Subject: [PATCH] Use new TelemetryDeck error logging --- .../ErrorHandling/Sources/ErrorHandler.swift | 18 +++++++++++++----- .../Tests/ErrorHandlerTests.swift | 11 +++++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Modules/Capabilities/ErrorHandling/Sources/ErrorHandler.swift b/Modules/Capabilities/ErrorHandling/Sources/ErrorHandler.swift index bdffe2cd..9356d3b2 100644 --- a/Modules/Capabilities/ErrorHandling/Sources/ErrorHandler.swift +++ b/Modules/Capabilities/ErrorHandling/Sources/ErrorHandler.swift @@ -27,15 +27,18 @@ public struct ErrorHandler: ErrorHandling { } public func log(_ error: Error) { - let errorDescription: String + let errorID: String if type(of: error) is NSError.Type { let nsError = error as NSError - errorDescription = "\(nsError.domain) - \(nsError.code): \(nsError.localizedDescription)" + errorID = "\(nsError.domain) - \(nsError.code)" } else { - errorDescription = String(describing: error) + errorID = String(describing: error) } - logger.log(Event(name: Self.logError, info: ["errorDescription": errorDescription])) + logger.log(Event(name: Self.logError, info: [ + Self.telemetryErrorIDKey: errorID, + Self.errorDescriptionKey: error.localizedDescription + ])) } public func crash(_ message: String) -> Never { @@ -50,9 +53,14 @@ public struct ErrorHandler: ErrorHandling { // MARK: Event Names - private static let logError = Event.Name("logError") + private static let logError = Event.Name("TelemetryDeck.Error.occurred") private static let crash = Event.Name("crash") private static let notImplemented = Event.Name("notImplemented") + + // MARK: Event Keys + + private static let errorDescriptionKey = "errorDescription" + private static let telemetryErrorIDKey = "TelemetryDeck.Error.id" } @objc(ErrorHandling) diff --git a/Modules/Capabilities/ErrorHandling/Tests/ErrorHandlerTests.swift b/Modules/Capabilities/ErrorHandling/Tests/ErrorHandlerTests.swift index 3c18b1b6..6f46a728 100644 --- a/Modules/Capabilities/ErrorHandling/Tests/ErrorHandlerTests.swift +++ b/Modules/Capabilities/ErrorHandling/Tests/ErrorHandlerTests.swift @@ -15,8 +15,8 @@ final class ErrorHandlerTests: XCTestCase { handler.log(SampleError.sample) let event = try XCTUnwrap(logger.loggedEvents.first) - XCTAssertEqual(event.value, "logError") - XCTAssertEqual(event.info, ["errorDescription": "sample"]) + XCTAssertEqual(event.value, "TelemetryDeck.Error.occurred") + XCTAssertEqual(event.info["TelemetryDeck.Error.id"], "sample") } func testLoggingNSErrorLogsInformation() throws { @@ -27,8 +27,11 @@ final class ErrorHandlerTests: XCTestCase { handler.log(error) let event = try XCTUnwrap(logger.loggedEvents.first) - XCTAssertEqual(event.value, "logError") - XCTAssertEqual(event.info, ["errorDescription": "sample - 19: The operation couldn’t be completed. (sample error 19.)"]) + XCTAssertEqual(event.value, "TelemetryDeck.Error.occurred") + XCTAssertEqual(event.info, [ + "TelemetryDeck.Error.id": "sample - 19", + "errorDescription": "The operation couldn’t be completed. (sample error 19.)" + ]) } func testCrashingLogsMessage() throws {