import os.log
enum PBLogger {
private static let log = Logger(subsystem: "com.example.app", category: "privacy-boost")
static func error(_ context: String, _ error: Error) {
guard let sdk = error as? SdkError else {
log.error("[\(context, privacy: .public)] non-sdk error: \(error.localizedDescription, privacy: .public)")
return
}
log.error("""
[\(context, privacy: .public)] \
category=\(String(describing: sdk.category), privacy: .public) \
retryable=\(sdk.isRetryable) \
variant=\(self.variantTag(sdk), privacy: .public) \
message=\(self.userMessage(sdk), privacy: .public)
""")
}
private static func variantTag(_ error: SdkError) -> String {
switch error {
case .NotConnected: return "NotConnected"
case .NotAuthenticated: return "NotAuthenticated"
case .AuthenticationFailed: return "AuthenticationFailed"
case .NetworkError: return "NetworkError"
case .WalletError: return "WalletError"
case .SignatureRejected: return "SignatureRejected"
case .InsufficientBalance: return "InsufficientBalance"
case .InvalidAddress: return "InvalidAddress"
case .InvalidAmount: return "InvalidAmount"
case .RateLimited: return "RateLimited"
case .ApiError(let code, _, _): return "ApiError.\(code)"
case .ShieldError(let code, _): return "ShieldError.\(code)"
case .TransferError(let code, _): return "TransferError.\(code)"
default: return "Other"
}
}
private static func userMessage(_ error: SdkError) -> String {
switch error {
case .NetworkError(let m), .WalletError(let m), .InternalError(let m),
.Forbidden(let m), .ResourceNotFound(let m):
return m
case .ApiError(_, let m, _), .ShieldError(_, let m), .TransferError(_, let m):
return m
default:
return ""
}
}
}