Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error wrapper to conform any error type to IdentifiableError #182

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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