@@ -36,6 +36,7 @@ var Client = function (config) {
36
36
this . _connecting = false
37
37
this . _connected = false
38
38
this . _connectionError = false
39
+ this . _queryable = true
39
40
40
41
this . connection = c . connection || new Connection ( {
41
42
stream : c . stream ,
@@ -126,15 +127,39 @@ Client.prototype.connect = function (callback) {
126
127
}
127
128
128
129
const connectedErrorHandler = ( err ) => {
130
+ this . _queryable = false
131
+
132
+ const enqueueError = ( query ) => {
133
+ process . nextTick ( ( ) => {
134
+ query . handleError ( err , con )
135
+ } )
136
+ }
137
+
129
138
if ( this . activeQuery ) {
130
- var activeQuery = self . activeQuery
139
+ enqueueError ( this . activeQuery )
131
140
this . activeQuery = null
132
- return activeQuery . handleError ( err , con )
133
141
}
142
+
143
+ this . queryQueue . forEach ( enqueueError )
144
+ this . queryQueue = [ ]
145
+
134
146
this . emit ( 'error' , err )
135
147
}
136
148
149
+ const connectedErrorMessageHandler = ( msg ) => {
150
+ const activeQuery = this . activeQuery
151
+
152
+ if ( ! activeQuery ) {
153
+ connectedErrorHandler ( msg )
154
+ return
155
+ }
156
+
157
+ this . activeQuery = null
158
+ activeQuery . handleError ( msg , con )
159
+ }
160
+
137
161
con . on ( 'error' , connectingErrorHandler )
162
+ con . on ( 'errorMessage' , connectingErrorHandler )
138
163
139
164
// hook up query handling events to connection
140
165
// after the connection initially becomes ready for queries
@@ -143,7 +168,9 @@ Client.prototype.connect = function (callback) {
143
168
self . _connected = true
144
169
self . _attachListeners ( con )
145
170
con . removeListener ( 'error' , connectingErrorHandler )
171
+ con . removeListener ( 'errorMessage' , connectingErrorHandler )
146
172
con . on ( 'error' , connectedErrorHandler )
173
+ con . on ( 'errorMessage' , connectedErrorMessageHandler )
147
174
148
175
// process possible callback argument to Client#connect
149
176
if ( callback ) {
@@ -353,7 +380,13 @@ Client.prototype._pulseQueryQueue = function () {
353
380
if ( this . activeQuery ) {
354
381
this . readyForQuery = false
355
382
this . hasExecuted = true
356
- this . activeQuery . submit ( this . connection )
383
+
384
+ const queryError = this . activeQuery . submit ( this . connection )
385
+ if ( queryError ) {
386
+ this . activeQuery . handleError ( queryError , this . connection )
387
+ this . readyForQuery = true
388
+ this . _pulseQueryQueue ( )
389
+ }
357
390
} else if ( this . hasExecuted ) {
358
391
this . activeQuery = null
359
392
this . emit ( 'drain' )
@@ -389,25 +422,36 @@ Client.prototype.query = function (config, values, callback) {
389
422
query . _result . _getTypeParser = this . _types . getTypeParser . bind ( this . _types )
390
423
}
391
424
425
+ if ( ! this . _queryable ) {
426
+ query . handleError ( new Error ( 'Client has encountered a connection error and is not queryable' ) , this . connection )
427
+ return
428
+ }
429
+
430
+ if ( this . _ending ) {
431
+ query . handleError ( new Error ( 'Client was closed and is not queryable' ) , this . connection )
432
+ return
433
+ }
434
+
392
435
this . queryQueue . push ( query )
393
436
this . _pulseQueryQueue ( )
394
437
return result
395
438
}
396
439
397
440
Client . prototype . end = function ( cb ) {
398
441
this . _ending = true
442
+
399
443
if ( this . activeQuery ) {
400
444
// if we have an active query we need to force a disconnect
401
445
// on the socket - otherwise a hung query could block end forever
402
- this . connection . stream . destroy ( new Error ( 'Connection terminated by user' ) )
403
- return cb ? cb ( ) : Promise . resolve ( )
446
+ this . connection . stream . destroy ( )
447
+ } else {
448
+ this . connection . end ( )
404
449
}
450
+
405
451
if ( cb ) {
406
- this . connection . end ( )
407
452
this . connection . once ( 'end' , cb )
408
453
} else {
409
- return new global . Promise ( ( resolve , reject ) => {
410
- this . connection . end ( )
454
+ return new Promise ( ( resolve ) => {
411
455
this . connection . once ( 'end' , resolve )
412
456
} )
413
457
}
0 commit comments