import Foundation #if canImport(FoundationNetworking) import FoundationNetworking #endif import PostgresNIO /// One line that says what went wrong, for the log. enum ErrorSummary { static let limit = 600 static func describe(_ error: any Error) -> String { if let error = error as? PSQLError { return describe(error) } // A URLError's reflection quotes the URL it failed on, and a URL can // carry what someone searched for. Its code says enough. if let error = error as? URLError { return "URLError \(error.code.rawValue)" } return String(String(reflecting: error).prefix(limit)) } /// PostgresNIO replaces `description` with a fixed sentence, and its full /// reflection includes the values bound to the failed query, which can be /// what a user wrote. This keeps what the server said and where it said /// it, and nothing that was bound. `detail` is left out for the same /// reason: a constraint failure quotes the whole row there. static func describe(_ error: PSQLError) -> String { var parts = ["postgres error", "code=\(error.code)"] if let info = error.serverInfo { let fields: [(String, PSQLError.ServerInfo.Field)] = [ ("sqlstate", .sqlState), ("message", .message), ("table", .tableName), ("column", .columnName), ("constraint", .constraintName), ("routine", .routine), ] for (name, field) in fields { if let value = info[field] { parts.append("\(name)=\(value)") } } } if let underlying = error.underlying { parts.append("underlying=\(String(reflecting: underlying).prefix(200))") } return parts.joined(separator: " ") } }