@@ -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+ }
0 commit comments