@@ -95,9 +95,9 @@ class Client extends EventEmitter {
95
95
}
96
96
this . _connecting = true
97
97
98
- var connectionTimeoutHandle
98
+ this . connectionTimeoutHandle
99
99
if ( this . _connectionTimeoutMillis > 0 ) {
100
- connectionTimeoutHandle = setTimeout ( ( ) => {
100
+ this . connectionTimeoutHandle = setTimeout ( ( ) => {
101
101
con . _ending = true
102
102
con . stream . destroy ( new Error ( 'timeout expired' ) )
103
103
} , this . _connectionTimeoutMillis )
@@ -133,35 +133,11 @@ class Client extends EventEmitter {
133
133
con . once ( 'backendKeyData' , this . handleBackendKeyData . bind ( this ) )
134
134
135
135
this . _connectionCallback = callback
136
- const connectingErrorHandler = ( err ) => {
137
- if ( this . _connectionError ) {
138
- return
139
- }
140
- this . _connectionError = true
141
- clearTimeout ( connectionTimeoutHandle )
142
- if ( this . _connectionCallback ) {
143
- return this . _connectionCallback ( err )
144
- }
145
- this . emit ( 'error' , err )
146
- }
147
-
148
- const connectedErrorHandler = ( err ) => {
149
- this . _queryable = false
150
- this . _errorAllQueries ( err )
151
- this . emit ( 'error' , err )
152
- }
136
+ const connectingErrorHandler = this . handleErrorWhileConnecting . bind ( this )
153
137
154
- const connectedErrorMessageHandler = ( msg ) => {
155
- const activeQuery = this . activeQuery
138
+ const connectedErrorHandler = this . handleErrorWhileConnected . bind ( this )
156
139
157
- if ( ! activeQuery ) {
158
- connectedErrorHandler ( msg )
159
- return
160
- }
161
-
162
- this . activeQuery = null
163
- activeQuery . handleError ( msg , con )
164
- }
140
+ const connectedErrorMessageHandler = this . handleErrorMessage . bind ( this )
165
141
166
142
con . on ( 'error' , connectingErrorHandler )
167
143
con . on ( 'errorMessage' , connectingErrorHandler )
@@ -175,7 +151,7 @@ class Client extends EventEmitter {
175
151
con . removeListener ( 'errorMessage' , connectingErrorHandler )
176
152
con . on ( 'error' , connectedErrorHandler )
177
153
con . on ( 'errorMessage' , connectedErrorMessageHandler )
178
- clearTimeout ( connectionTimeoutHandle )
154
+ clearTimeout ( this . connectionTimeoutHandle )
179
155
180
156
// process possible callback argument to Client#connect
181
157
if ( this . _connectionCallback ) {
@@ -194,7 +170,7 @@ class Client extends EventEmitter {
194
170
con . once ( 'end' , ( ) => {
195
171
const error = this . _ending ? new Error ( 'Connection terminated' ) : new Error ( 'Connection terminated unexpectedly' )
196
172
197
- clearTimeout ( connectionTimeoutHandle )
173
+ clearTimeout ( this . connectionTimeoutHandle )
198
174
this . _errorAllQueries ( error )
199
175
200
176
if ( ! this . _ending ) {
@@ -331,6 +307,42 @@ class Client extends EventEmitter {
331
307
this . _pulseQueryQueue ( )
332
308
}
333
309
310
+ // if we receieve an error during the connection process we handle it here
311
+ handleErrorWhileConnecting ( err ) {
312
+ if ( this . _connectionError ) {
313
+ // TODO(bmc): this is swallowing errors - we shouldn't do this
314
+ return
315
+ }
316
+ this . _connectionError = true
317
+ clearTimeout ( this . connectionTimeoutHandle )
318
+ if ( this . _connectionCallback ) {
319
+ return this . _connectionCallback ( err )
320
+ }
321
+ this . emit ( 'error' , err )
322
+ }
323
+
324
+ // if we're connected and we receive an error event from the connection
325
+ // this means the socket is dead - do a hard abort of all queries and emit
326
+ // the socket error on the client as well
327
+ handleErrorWhileConnected ( err ) {
328
+ this . _queryable = false
329
+ this . _errorAllQueries ( err )
330
+ this . emit ( 'error' , err )
331
+ }
332
+
333
+ // handle error messages from the postgres backend
334
+ handleErrorMessage ( msg ) {
335
+ const activeQuery = this . activeQuery
336
+
337
+ if ( ! activeQuery ) {
338
+ this . handleErrorWhileConnected ( msg )
339
+ return
340
+ }
341
+
342
+ this . activeQuery = null
343
+ activeQuery . handleError ( msg , this . connection )
344
+ }
345
+
334
346
handleRowDescription ( msg ) {
335
347
// delegate rowDescription to active query
336
348
this . activeQuery . handleRowDescription ( msg )
0 commit comments