@@ -95,9 +95,9 @@ class Client extends EventEmitter {
9595 }
9696 this . _connecting = true
9797
98- var connectionTimeoutHandle
98+ this . connectionTimeoutHandle
9999 if ( this . _connectionTimeoutMillis > 0 ) {
100- connectionTimeoutHandle = setTimeout ( ( ) => {
100+ this . connectionTimeoutHandle = setTimeout ( ( ) => {
101101 con . _ending = true
102102 con . stream . destroy ( new Error ( 'timeout expired' ) )
103103 } , this . _connectionTimeoutMillis )
@@ -133,35 +133,11 @@ class Client extends EventEmitter {
133133 con . once ( 'backendKeyData' , this . handleBackendKeyData . bind ( this ) )
134134
135135 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 )
153137
154- const connectedErrorMessageHandler = ( msg ) => {
155- const activeQuery = this . activeQuery
138+ const connectedErrorHandler = this . handleErrorWhileConnected . bind ( this )
156139
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 )
165141
166142 con . on ( 'error' , connectingErrorHandler )
167143 con . on ( 'errorMessage' , connectingErrorHandler )
@@ -175,7 +151,7 @@ class Client extends EventEmitter {
175151 con . removeListener ( 'errorMessage' , connectingErrorHandler )
176152 con . on ( 'error' , connectedErrorHandler )
177153 con . on ( 'errorMessage' , connectedErrorMessageHandler )
178- clearTimeout ( connectionTimeoutHandle )
154+ clearTimeout ( this . connectionTimeoutHandle )
179155
180156 // process possible callback argument to Client#connect
181157 if ( this . _connectionCallback ) {
@@ -194,7 +170,7 @@ class Client extends EventEmitter {
194170 con . once ( 'end' , ( ) => {
195171 const error = this . _ending ? new Error ( 'Connection terminated' ) : new Error ( 'Connection terminated unexpectedly' )
196172
197- clearTimeout ( connectionTimeoutHandle )
173+ clearTimeout ( this . connectionTimeoutHandle )
198174 this . _errorAllQueries ( error )
199175
200176 if ( ! this . _ending ) {
@@ -331,6 +307,42 @@ class Client extends EventEmitter {
331307 this . _pulseQueryQueue ( )
332308 }
333309
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+
334346 handleRowDescription ( msg ) {
335347 // delegate rowDescription to active query
336348 this . activeQuery . handleRowDescription ( msg )
0 commit comments