@@ -3,7 +3,7 @@ import * as net from 'net';
3
3
import * as tls from 'tls' ;
4
4
import { encodeCommand } from '../commander' ;
5
5
import { RedisCommandArguments } from '../commands' ;
6
- import { ConnectionTimeoutError , ClientClosedError , SocketClosedUnexpectedlyError , AuthError } from '../errors' ;
6
+ import { ConnectionTimeoutError , ClientClosedError , SocketClosedUnexpectedlyError , AuthError , ReconnectStrategyError } from '../errors' ;
7
7
import { promiseTimeout } from '../utils' ;
8
8
9
9
export interface RedisSocketCommonOptions {
@@ -93,9 +93,16 @@ export default class RedisSocket extends EventEmitter {
93
93
}
94
94
95
95
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
+ }
99
106
100
107
if ( ! this . #isOpen) {
101
108
this . disconnect ( ) ;
@@ -134,17 +141,16 @@ export default class RedisSocket extends EventEmitter {
134
141
try {
135
142
return await this . #createSocket( ) ;
136
143
} catch ( err ) {
137
- this . emit ( 'error' , err ) ;
138
-
139
144
if ( ! this . #isOpen) {
140
145
throw err ;
141
146
}
142
147
143
148
const retryIn = ( this . #options?. reconnectStrategy ?? RedisSocket . #defaultReconnectStrategy) ( retries ) ;
144
149
if ( retryIn instanceof Error ) {
145
- throw retryIn ;
150
+ throw new ReconnectStrategyError ( retryIn , err ) ;
146
151
}
147
152
153
+ this . emit ( 'error' , err ) ;
148
154
await promiseTimeout ( retryIn ) ;
149
155
return this . #retryConnection( retries + 1 ) ;
150
156
}
0 commit comments