@@ -31,6 +31,9 @@ var Client = function(config) {
31
31
var c = config || { } ;
32
32
33
33
this . _types = new TypeOverrides ( c . types ) ;
34
+ this . _ending = false ;
35
+ this . _connecting = false ;
36
+ this . _connectionError = false ;
34
37
35
38
this . connection = c . connection || new Connection ( {
36
39
stream : c . stream ,
@@ -50,6 +53,7 @@ util.inherits(Client, EventEmitter);
50
53
Client . prototype . connect = function ( callback ) {
51
54
var self = this ;
52
55
var con = this . connection ;
56
+ this . _connecting = true ;
53
57
54
58
if ( this . host && this . host . indexOf ( '/' ) === 0 ) {
55
59
con . connect ( this . host + '/.s.PGSQL.' + this . port ) ;
@@ -107,6 +111,7 @@ Client.prototype.connect = function(callback) {
107
111
//hook up query handling events to connection
108
112
//after the connection initially becomes ready for queries
109
113
con . once ( 'readyForQuery' , function ( ) {
114
+ self . _connecting = false ;
110
115
111
116
//delegate rowDescription to active query
112
117
con . on ( 'rowDescription' , function ( msg ) {
@@ -175,34 +180,52 @@ Client.prototype.connect = function(callback) {
175
180
} ) ;
176
181
177
182
con . on ( 'error' , function ( error ) {
178
- if ( self . activeQuery ) {
183
+ if ( this . activeQuery ) {
179
184
var activeQuery = self . activeQuery ;
180
- self . activeQuery = null ;
185
+ this . activeQuery = null ;
181
186
return activeQuery . handleError ( error , con ) ;
182
187
}
188
+
189
+ if ( this . _connecting ) {
190
+ // set a flag indicating we've seen an error during connection
191
+ // the backend will terminate the connection and we don't want
192
+ // to throw a second error when the connection is terminated
193
+ this . _connectionError = true ;
194
+ }
195
+
183
196
if ( ! callback ) {
184
- return self . emit ( 'error' , error ) ;
197
+ return this . emit ( 'error' , error ) ;
185
198
}
199
+
186
200
con . end ( ) ; // make sure ECONNRESET errors don't cause error events
187
201
callback ( error ) ;
188
202
callback = null ;
189
- } ) ;
203
+ } . bind ( this ) ) ;
190
204
191
205
con . once ( 'end' , function ( ) {
192
- if ( callback ) {
193
- // haven't received a connection message yet !
206
+ if ( callback ) {
207
+ // haven't received a connection message yet!
194
208
var err = new Error ( 'Connection terminated' ) ;
195
209
callback ( err ) ;
196
210
callback = null ;
197
211
return ;
198
212
}
199
- if ( self . activeQuery ) {
213
+ if ( this . activeQuery ) {
200
214
var disconnectError = new Error ( 'Connection terminated' ) ;
201
- self . activeQuery . handleError ( disconnectError , con ) ;
202
- self . activeQuery = null ;
215
+ this . activeQuery . handleError ( disconnectError , con ) ;
216
+ this . activeQuery = null ;
203
217
}
204
- self . emit ( 'end' ) ;
205
- } ) ;
218
+ if ( ! this . _ending ) {
219
+ // if the connection is ended without us calling .end()
220
+ // on this client then we have an unexpected disconnection
221
+ // treat this as an error unless we've already emitted an error
222
+ // during connection.
223
+ if ( ! this . _connectionError ) {
224
+ this . emit ( 'error' , new Error ( 'Connection terminated unexpectedly' ) ) ;
225
+ }
226
+ }
227
+ this . emit ( 'end' ) ;
228
+ } . bind ( this ) ) ;
206
229
207
230
208
231
con . on ( 'notice' , function ( msg ) {
@@ -342,6 +365,7 @@ Client.prototype.query = function(config, values, callback) {
342
365
} ;
343
366
344
367
Client . prototype . end = function ( cb ) {
368
+ this . _ending = true ;
345
369
this . connection . end ( ) ;
346
370
if ( cb ) {
347
371
this . connection . once ( 'end' , cb ) ;
0 commit comments