Skip to content

Commit

Permalink
Merge pull request #182 from TelemetryDeck/feature/any-error
Browse files Browse the repository at this point in the history
Add error wrapper to conform any error type to IdentifiableError
  • Loading branch information
Jeehut authored Sep 9, 2024
2 parents eba444d + 811c8f2 commit 2c9ec7c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions Sources/TelemetryClient/Presets/AnyIdentifiableError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Foundation

/// A generic wrapper that conforms any error to ``IdentifiableError``, exposing its `localizedDescription` as the `message`.
public struct AnyIdentifiableError: LocalizedError, IdentifiableError {
/// Unique identifier for the error, such as `TelemetryDeck.Session.started`.
public let id: String

/// The underlying error being wrapped.
public let error: any Error

/// Initializes with a given `id` and `error`.
/// - Parameters:
/// - id: Unique identifier for the error, such as `TelemetryDeck.Session.started`.
/// - error: The error to be wrapped.
public init(id: String, error: any Error) {
self.id = id
self.error = error
}

/// Provides the localized description of the wrapped error.
public var errorDescription: String {
self.error.localizedDescription
}
}

extension Error {
/// Wraps any caught error with an `id` for use with ``TelemetryDeck.signal(identifiableError:)``.
/// - Parameters:
/// - id: Unique identifier for the error, such as `TelemetryDeck.Session.started`.
/// - Returns: An ``AnyIdentifiableError`` instance wrapping the given error.
public func with(id: String) -> AnyIdentifiableError {
AnyIdentifiableError(id: id, error: self)
}
}
2 changes: 1 addition & 1 deletion Sources/TelemetryClient/Presets/TelemetryDeck+Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public extension TelemetryDeck {
/// Sends a telemetry signal indicating that an identifiable error has occurred.
///
/// - Parameters:
/// - identifiableError: The error that conforms to `IdentifiableError`.
/// - identifiableError: The error that conforms to `IdentifiableError`. Conform any error type by calling `.with(id:)` on it.
/// - category: The category of the error. Default is `.thrownException`.
/// - parameters: Additional parameters to include with the signal. Default is an empty dictionary.
/// - floatValue: An optional floating-point value to include with the signal. Default is `nil`.
Expand Down

0 comments on commit 2c9ec7c

Please sign in to comment.