@@ -3,7 +3,7 @@ import * as net from 'net';
33import * as tls from 'tls' ;
44import { encodeCommand } from '../commander' ;
55import { RedisCommandArguments } from '../commands' ;
6- import { ConnectionTimeoutError , ClientClosedError , SocketClosedUnexpectedlyError , AuthError } from '../errors' ;
6+ import { ConnectionTimeoutError , ClientClosedError , SocketClosedUnexpectedlyError , AuthError , ReconnectStrategyError } from '../errors' ;
77import { promiseTimeout } from '../utils' ;
88
99export interface RedisSocketCommonOptions {
@@ -93,9 +93,16 @@ export default class RedisSocket extends EventEmitter {
9393 }
9494
9595 async #connect( hadError ?: boolean ) : Promise < void > {
96- this . #isOpen = true ;
97- this . #socket = await this . #retryConnection( 0 , hadError ) ;
98- this . #writableNeedDrain = false ;
96+ try {
97+ this . #isOpen = true ;
98+ this . #socket = await this . #retryConnection( 0 , hadError ) ;
99+ this . #writableNeedDrain = false ;
100+ } catch ( err ) {
101+ this . #isOpen = false ;
102+ this . emit ( 'error' , err ) ;
103+ this . emit ( 'end' ) ;
104+ throw err ;
105+ }
99106
100107 if ( ! this . #isOpen) {
101108 this . disconnect ( ) ;
@@ -134,17 +141,16 @@ export default class RedisSocket extends EventEmitter {
134141 try {
135142 return await this . #createSocket( ) ;
136143 } catch ( err ) {
137- this . emit ( 'error' , err ) ;
138-
139144 if ( ! this . #isOpen) {
140145 throw err ;
141146 }
142147
143148 const retryIn = ( this . #options?. reconnectStrategy ?? RedisSocket . #defaultReconnectStrategy) ( retries ) ;
144149 if ( retryIn instanceof Error ) {
145- throw retryIn ;
150+ throw new ReconnectStrategyError ( retryIn , err ) ;
146151 }
147152
153+ this . emit ( 'error' , err ) ;
148154 await promiseTimeout ( retryIn ) ;
149155 return this . #retryConnection( retries + 1 ) ;
150156 }
0 commit comments