Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary bandaid workaround for lack of PSQLError usefulness #244

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/PostgresKit/PostgresConnectionSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ public struct PostgresConnectionSource: ConnectionPoolSource {
let string = searchPath.map { #""\#($0)""# }.joined(separator: ", ")
return conn.simpleQuery("SET search_path = \(string)").map { _ in conn }
}
.flatMapErrorThrowing { try PostgresDataTranslation.applyPSQLErrorBandaidIfNeeded(for: $0) }
} else {
return connectionFuture
.flatMapErrorThrowing { try PostgresDataTranslation.applyPSQLErrorBandaidIfNeeded(for: $0) }
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions Sources/PostgresKit/PostgresDatabase+SQL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,44 @@ extension _PostgresSQLDatabase: SQLDatabase, PostgresDatabase {

func send(_ request: any PostgresRequest, logger: Logger) -> EventLoopFuture<Void> {
self.database.send(request, logger: logger)
.flatMapErrorThrowing { try PostgresDataTranslation.applyPSQLErrorBandaidIfNeeded(for: $0) }
}

func withConnection<T>(_ closure: @escaping (PostgresConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
self.database.withConnection(closure)
.flatMapErrorThrowing { try PostgresDataTranslation.applyPSQLErrorBandaidIfNeeded(for: $0) }
}
}

// This can go away as soon as https://github.com/vapor/postgres-nio/pull/360 is merged.
struct ExpressivePSQLError: Error, CustomStringConvertible, CustomDebugStringConvertible {
let underlyingError: PSQLError
var description: String { "Database error" }
var debugDescription: String {
var desc = #"PSQLError(code: \#(self.underlyingError.code)"#
if let serverInfo = self.underlyingError.serverInfo {
desc.append(", serverInfo: [")
desc.append(["localizedSeverity": PSQLError.ServerInfo.Field.localizedSeverity, "severity": .severity, "sqlState": .sqlState, "message": .message, "detail": .detail, "hint": .hint, "position": .position, "internalPosition": .internalPosition, "internalQuery": .internalQuery, "locationContext": .locationContext, "schemaName": .schemaName, "tableName": .tableName, "columnName": .columnName, "dataTypeName": .dataTypeName, "constraintName": .constraintName, "file": .file, "line": .line, "routine": .routine].compactMap { name, field in serverInfo[field].map { "\(name): \($0)" } }.joined(separator: ", "))
desc.append("]")
}
if let underlying = self.underlyingError.underlying { desc.append(", underlying: \(String(reflecting: underlying))") }
if let file = self.underlyingError.file {
desc.append(", triggeredFromRequestInFile: \(file)")
if let line = self.underlyingError.line { desc.append(", line: \(line)") }
}
if let query = self.underlyingError.query { desc.append(", query: \(query)") }
desc.append(")")
return desc
}
}

extension PostgresDataTranslation {
@usableFromInline
static func applyPSQLErrorBandaidIfNeeded(for error: any Error) throws -> Never {
if let psqlError = error as? PSQLError {
throw ExpressivePSQLError(underlyingError: psqlError)
} else {
throw error
}
}
}