Skip to content

Commit

Permalink
Use new TelemetryDeck error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Arclite committed Aug 3, 2024
1 parent 79640d6 commit c1846ca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
18 changes: 13 additions & 5 deletions Modules/Capabilities/ErrorHandling/Sources/ErrorHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 40 in Modules/Capabilities/ErrorHandling/Sources/ErrorHandler.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Multi-line collection literals should have trailing commas (trailing_comma)
]))
}

public func crash(_ message: String) -> Never {
Expand All @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions Modules/Capabilities/ErrorHandling/Tests/ErrorHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.)"

Check failure on line 33 in Modules/Capabilities/ErrorHandling/Tests/ErrorHandlerTests.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Multi-line collection literals should have trailing commas (trailing_comma)
])
}

func testCrashingLogsMessage() throws {
Expand Down

0 comments on commit c1846ca

Please sign in to comment.