Skip to content

Commit 18a60ef

Browse files
authored
Make PSQLError public (#342)
1 parent f21252b commit 18a60ef

18 files changed

+453
-137
lines changed

Sources/PostgresNIO/Connection/PostgresConnection.swift

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public final class PostgresConnection: @unchecked Sendable {
283283
case is PSQLError:
284284
throw error
285285
default:
286-
throw PSQLError.channel(underlying: error)
286+
throw PSQLError.connectionError(underlying: error)
287287
}
288288
}
289289
}
@@ -312,7 +312,7 @@ public final class PostgresConnection: @unchecked Sendable {
312312
var logger = logger
313313
logger[postgresMetadataKey: .connectionID] = "\(self.id)"
314314
guard query.binds.count <= Int(UInt16.max) else {
315-
return self.channel.eventLoop.makeFailedFuture(PSQLError.tooManyParameters)
315+
return self.channel.eventLoop.makeFailedFuture(PSQLError(code: .tooManyParameters, query: query))
316316
}
317317

318318
let promise = self.channel.eventLoop.makePromise(of: PSQLRowStream.self)
@@ -344,7 +344,7 @@ public final class PostgresConnection: @unchecked Sendable {
344344

345345
func execute(_ executeStatement: PSQLExecuteStatement, logger: Logger) -> EventLoopFuture<PSQLRowStream> {
346346
guard executeStatement.binds.count <= Int(UInt16.max) else {
347-
return self.channel.eventLoop.makeFailedFuture(PSQLError.tooManyParameters)
347+
return self.channel.eventLoop.makeFailedFuture(PSQLError(code: .tooManyParameters))
348348
}
349349
let promise = self.channel.eventLoop.makePromise(of: PSQLRowStream.self)
350350
let context = ExtendedQueryContext(
@@ -493,14 +493,14 @@ extension PostgresConnection {
493493
public func query(
494494
_ query: PostgresQuery,
495495
logger: Logger,
496-
file: String = #file,
496+
file: String = #fileID,
497497
line: Int = #line
498498
) async throws -> PostgresRowSequence {
499499
var logger = logger
500500
logger[postgresMetadataKey: .connectionID] = "\(self.id)"
501501

502502
guard query.binds.count <= Int(UInt16.max) else {
503-
throw PSQLError.tooManyParameters
503+
throw PSQLError(code: .tooManyParameters, query: query, file: file, line: line)
504504
}
505505
let promise = self.channel.eventLoop.makePromise(of: PSQLRowStream.self)
506506
let context = ExtendedQueryContext(
@@ -511,7 +511,14 @@ extension PostgresConnection {
511511

512512
self.channel.write(PSQLTask.extendedQuery(context), promise: nil)
513513

514-
return try await promise.futureResult.map({ $0.asyncSequence() }).get()
514+
do {
515+
return try await promise.futureResult.map({ $0.asyncSequence() }).get()
516+
} catch var error as PSQLError {
517+
error.file = file
518+
error.line = line
519+
error.query = query
520+
throw error // rethrow with more metadata
521+
}
515522
}
516523
}
517524

@@ -530,7 +537,7 @@ extension PostgresConnection {
530537
public func query(
531538
_ query: PostgresQuery,
532539
logger: Logger,
533-
file: String = #file,
540+
file: String = #fileID,
534541
line: Int = #line
535542
) -> EventLoopFuture<PostgresQueryResult> {
536543
self.queryStream(query, logger: logger).flatMap { rowStream in
@@ -540,7 +547,7 @@ extension PostgresConnection {
540547
}
541548
return PostgresQueryResult(metadata: metadata, rows: rows)
542549
}
543-
}
550+
}.enrichPSQLError(query: query, file: file, line: line)
544551
}
545552

546553
/// Run a query on the Postgres server the connection is connected to and iterate the rows in a callback.
@@ -557,7 +564,7 @@ extension PostgresConnection {
557564
public func query(
558565
_ query: PostgresQuery,
559566
logger: Logger,
560-
file: String = #file,
567+
file: String = #fileID,
561568
line: Int = #line,
562569
_ onRow: @escaping (PostgresRow) throws -> ()
563570
) -> EventLoopFuture<PostgresQueryMetadata> {
@@ -568,7 +575,7 @@ extension PostgresConnection {
568575
}
569576
return metadata
570577
}
571-
}
578+
}.enrichPSQLError(query: query, file: file, line: line)
572579
}
573580
}
574581

@@ -785,3 +792,18 @@ extension PostgresConnection.InternalConfiguration {
785792
self.requireBackendKeyData = config.connection.requireBackendKeyData
786793
}
787794
}
795+
796+
extension EventLoopFuture {
797+
func enrichPSQLError(query: PostgresQuery, file: String, line: Int) -> EventLoopFuture<Value> {
798+
return self.flatMapErrorThrowing { error in
799+
if var error = error as? PSQLError {
800+
error.file = file
801+
error.line = line
802+
error.query = query
803+
throw error
804+
} else {
805+
throw error
806+
}
807+
}
808+
}
809+
}

Sources/PostgresNIO/New/Connection State Machine/AuthenticationStateMachine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct AuthenticationStateMachine {
5151
return .authenticated
5252
case .md5(let salt):
5353
guard self.authContext.password != nil else {
54-
return self.setAndFireError(.authMechanismRequiresPassword)
54+
return self.setAndFireError(PSQLError(code: .authMechanismRequiresPassword))
5555
}
5656
self.state = .passwordAuthenticationSent
5757
return .sendPassword(.md5(salt: salt), self.authContext)

Sources/PostgresNIO/New/Connection State Machine/ConnectionStateMachine.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,15 +1076,15 @@ extension ConnectionStateMachine {
10761076

10771077
extension ConnectionStateMachine {
10781078
func shouldCloseConnection(reason error: PSQLError) -> Bool {
1079-
switch error.base {
1079+
switch error.code.base {
10801080
case .sslUnsupported:
10811081
return true
10821082
case .failedToAddSSLHandler:
10831083
return true
10841084
case .queryCancelled:
10851085
return false
1086-
case .server(let message):
1087-
guard let sqlState = message.fields[.sqlState] else {
1086+
case .server:
1087+
guard let sqlState = error.serverInfo?[.sqlState] else {
10881088
// any error message that doesn't have a sql state field, is unexpected by default.
10891089
return true
10901090
}
@@ -1095,7 +1095,7 @@ extension ConnectionStateMachine {
10951095
}
10961096

10971097
return false
1098-
case .decoding:
1098+
case .messageDecodingFailure:
10991099
return true
11001100
case .unexpectedBackendMessage:
11011101
return true
@@ -1115,8 +1115,6 @@ extension ConnectionStateMachine {
11151115
preconditionFailure("Pure client error, that is thrown directly and should never ")
11161116
case .connectionError:
11171117
return true
1118-
case .casting(_):
1119-
preconditionFailure("Pure client error, that is thrown directly in PSQLRows")
11201118
case .uncleanShutdown:
11211119
return true
11221120
}
@@ -1142,7 +1140,7 @@ extension ConnectionStateMachine {
11421140
self.state = .error(error)
11431141

11441142
var action = ConnectionAction.CleanUpContext.Action.close
1145-
if case .uncleanShutdown = error.base {
1143+
if case .uncleanShutdown = error.code.base {
11461144
action = .fireChannelInactive
11471145
}
11481146

0 commit comments

Comments
 (0)