Skip to content

Commit

Permalink
Log the full error instead of only the error type.
Browse files Browse the repository at this point in the history
The retry implementation cannot know whether or not the error contains private information, so it should log the error and allow the user of the library to decide. As detailed [here](apple/swift-log#204 (comment)), the log handler can redact metadata values as needed.
  • Loading branch information
fumoboy007 committed Jun 5, 2024
1 parent 416667f commit df9d7b1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 11 additions & 0 deletions Sources/Retry/Logger+RetryMetadataKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,20 @@ extension Logger {
/// The one-based attempt number.
case attemptNumber = "retry.attempt"

/// The error that caused the attempt to fail.
///
/// This is the original error after removing wrapper types like ``Retryable`` and ``NotRetryable``.
///
/// - Warning: The error may contain private information. Consider redacting the metadata value later in
/// the logging pipeline before the log message is persisted or transmitted. The less sensitive
/// ``errorType`` metadata value may be sufficient.
case error = "retry.error"

/// The Swift type of the error that caused the attempt to fail.
///
/// This is the original error after removing wrapper types like ``Retryable`` and ``NotRetryable``.
///
/// - SeeAlso: ``error``
case errorType = "retry.error.type"

/// The delay before the next attempt.
Expand Down
4 changes: 1 addition & 3 deletions Sources/Retry/Retry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,7 @@ public func retry<ClockType, ReturnType>(
}

logger?[metadataKey: .attemptNumber] = "\(attempt)"
// Only log the error type rather than the full error in case the error has private user data.
// We can include the full error if and when the `Logging` API offers a distinction between
// public and private data.
logger?[metadataKey: .error] = "\(latestError)"
logger?[metadataKey: .errorType] = "\(type(of: latestError))"

switch recoveryAction {
Expand Down

0 comments on commit df9d7b1

Please sign in to comment.