@@ -78,6 +78,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
7878
7979 let socket = null
8080 , cancelMessage
81+ , errorResponse = null
8182 , result = new Result ( )
8283 , incoming = Buffer . alloc ( 0 )
8384 , needsTypes = options . fetch_types
@@ -155,10 +156,10 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
155156 function execute ( q ) {
156157 if ( terminated )
157158 return queryError ( q , Errors . connection ( 'CONNECTION_DESTROYED' , options ) )
158-
159+
159160 if ( stream )
160161 return queryError ( q , Errors . generic ( 'COPY_IN_PROGRESS' , 'You cannot execute queries during copy' ) )
161-
162+
162163 if ( q . cancelled )
163164 return
164165
@@ -266,24 +267,24 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
266267 if ( sslnegotiation !== 'direct' ) {
267268 write ( SSLRequest )
268269 const canSSL = await new Promise ( r => socket . once ( 'data' , x => r ( x [ 0 ] === 83 ) ) ) // S
269-
270+
270271 if ( ! canSSL && ssl === 'prefer' )
271272 return connected ( )
272273 }
273-
274+
274275 const options = {
275276 socket,
276- servername : net . isIP ( socket . host ) ? undefined : socket . host ,
277+ servername : net . isIP ( socket . host ) ? undefined : socket . host
277278 }
278-
279+
279280 if ( sslnegotiation === 'direct' )
280281 options . ALPNProtocols = [ 'postgresql' ]
281-
282+
282283 if ( ssl === 'require' || ssl === 'allow' || ssl === 'prefer' )
283284 options . rejectUnauthorized = false
284285 else if ( typeof ssl === 'object' )
285286 Object . assign ( options , ssl )
286-
287+
287288 socket . removeAllListeners ( )
288289 socket = tls . connect ( options )
289290 socket . on ( 'secureConnect' , connected )
@@ -532,8 +533,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
532533 }
533534
534535 function ReadyForQuery ( x ) {
535- query && query . options . simple && query . resolve ( results || result )
536- query = results = null
536+ if ( query ) {
537+ if ( errorResponse ) {
538+ query . retried
539+ ? errored ( query . retried )
540+ : query . prepared && retryRoutines . has ( errorResponse . routine )
541+ ? retry ( query , errorResponse )
542+ : errored ( errorResponse )
543+ } else {
544+ query . resolve ( results || result )
545+ }
546+ } else if ( errorResponse ) {
547+ errored ( errorResponse )
548+ }
549+
550+ query = results = errorResponse = null
537551 result = new Result ( )
538552 connectTimer . cancel ( )
539553
@@ -598,8 +612,6 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
598612 result . count && query . cursorFn ( result )
599613 write ( Sync )
600614 }
601-
602- query . resolve ( result )
603615 }
604616
605617 function ParseComplete ( ) {
@@ -798,13 +810,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
798810 }
799811
800812 function ErrorResponse ( x ) {
801- query && ( query . cursorFn || query . describeFirst ) && write ( Sync )
802- const error = Errors . postgres ( parseError ( x ) )
803- query && query . retried
804- ? errored ( query . retried )
805- : query && query . prepared && retryRoutines . has ( error . routine )
806- ? retry ( query , error )
807- : errored ( error )
813+ if ( query ) {
814+ ( query . cursorFn || query . describeFirst ) && write ( Sync )
815+ errorResponse = Errors . postgres ( parseError ( x ) )
816+ } else {
817+ errored ( Errors . postgres ( parseError ( x ) ) )
818+ }
808819 }
809820
810821 function retry ( q , error ) {
0 commit comments