-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from TelemetryDeck/feature/any-error
Add error wrapper to conform any error type to IdentifiableError
- Loading branch information
Showing
2 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
Sources/TelemetryClient/Presets/AnyIdentifiableError.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters