Skip to content

Commit

Permalink
Update Pixel.swift to recursively read underlying error parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
samsymons committed Nov 20, 2024
1 parent 20c6e11 commit 7923527
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions Core/Pixel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,37 @@ private extension Pixel.Event {
}

extension Dictionary where Key == String, Value == String {

mutating func appendErrorPixelParams(error: Error) {
let nsError = error as NSError

self[PixelParameters.errorCode] = "\(nsError.code)"
self[PixelParameters.errorDomain] = nsError.domain

if let underlyingError = nsError.userInfo["NSUnderlyingError"] as? NSError {
self[PixelParameters.underlyingErrorCode] = "\(underlyingError.code)"
self[PixelParameters.underlyingErrorDomain] = underlyingError.domain
let underlyingErrorParameters = underlyingErrorParameters(for: error as NSError)
self.merge(underlyingErrorParameters) { first, _ in first }
}

private func underlyingErrorParameters(for nsError: NSError, level: Int = 0) -> [String: String] {
if let underlyingError = nsError.userInfo[NSUnderlyingErrorKey] as? NSError {
let errorCodeParameterName = PixelParameters.underlyingErrorCode + (level == 0 ? "" : String(level + 1))
let errorDomainParameterName = PixelParameters.underlyingErrorDomain + (level == 0 ? "" : String(level + 1))

let currentUnderlyingErrorParameters = [
errorCodeParameterName: "\(underlyingError.code)",
errorDomainParameterName: underlyingError.domain
]

let additionalParameters = underlyingErrorParameters(for: underlyingError, level: level + 1)
return currentUnderlyingErrorParameters.merging(additionalParameters) { first, _ in first }
} else if let sqlErrorCode = nsError.userInfo["NSSQLiteErrorDomain"] as? NSNumber {
self[PixelParameters.underlyingErrorCode] = "\(sqlErrorCode.intValue)"
self[PixelParameters.underlyingErrorDomain] = "NSSQLiteErrorDomain"
return [
PixelParameters.underlyingErrorCode: "\(sqlErrorCode.intValue)",
PixelParameters.underlyingErrorDomain: "NSSQLiteErrorDomain"
]
}

return [:]
}

}

0 comments on commit 7923527

Please sign in to comment.