@@ -174,6 +174,8 @@ export class RedisConnectionFactory {
174
174
175
175
return await new Promise ( ( resolve , reject ) => {
176
176
try {
177
+ let lastError : Error ;
178
+
177
179
if ( tnl ) {
178
180
tnl . on ( 'error' , ( error ) => {
179
181
reject ( error ) ;
@@ -194,18 +196,20 @@ export class RedisConnectionFactory {
194
196
} ) ;
195
197
connection . on ( 'error' , ( e ) : void => {
196
198
this . logger . error ( 'Failed connection to the redis database.' , e ) ;
197
- reject ( e ) ;
199
+ lastError = e ;
198
200
} ) ;
199
201
connection . on ( 'end' , ( ) : void => {
200
- this . logger . error ( ERROR_MESSAGES . SERVER_CLOSED_CONNECTION ) ;
201
- reject ( new InternalServerErrorException ( ERROR_MESSAGES . SERVER_CLOSED_CONNECTION ) ) ;
202
+ this . logger . error ( ERROR_MESSAGES . UNABLE_TO_ESTABLISH_CONNECTION , lastError ) ;
203
+ reject ( lastError || new InternalServerErrorException ( ERROR_MESSAGES . SERVER_CLOSED_CONNECTION ) ) ;
202
204
} ) ;
203
205
connection . on ( 'ready' , ( ) : void => {
206
+ lastError = null ;
204
207
this . logger . log ( 'Successfully connected to the redis database' ) ;
205
208
resolve ( connection ) ;
206
209
} ) ;
207
210
connection . on ( 'reconnecting' , ( ) : void => {
208
- this . logger . log ( 'Reconnecting to the redis database' ) ;
211
+ lastError = null ;
212
+ this . logger . log ( ERROR_MESSAGES . RECONNECTING_TO_DATABASE ) ;
209
213
} ) ;
210
214
} catch ( e ) {
211
215
reject ( e ) ;
@@ -236,6 +240,8 @@ export class RedisConnectionFactory {
236
240
237
241
return new Promise ( ( resolve , reject ) => {
238
242
try {
243
+ let lastError : Error ;
244
+
239
245
const cluster = new Cluster ( [ {
240
246
host : database . host ,
241
247
port : database . port ,
@@ -244,16 +250,21 @@ export class RedisConnectionFactory {
244
250
} ) ;
245
251
cluster . on ( 'error' , ( e ) : void => {
246
252
this . logger . error ( 'Failed connection to the redis oss cluster' , e ) ;
247
- reject ( ! isEmpty ( e . lastNodeError ) ? e . lastNodeError : e ) ;
253
+ lastError = ! isEmpty ( e . lastNodeError ) ? e . lastNodeError : e ;
248
254
} ) ;
249
255
cluster . on ( 'end' , ( ) : void => {
250
- this . logger . error ( ERROR_MESSAGES . SERVER_CLOSED_CONNECTION ) ;
251
- reject ( new InternalServerErrorException ( ERROR_MESSAGES . SERVER_CLOSED_CONNECTION ) ) ;
256
+ this . logger . error ( ERROR_MESSAGES . UNABLE_TO_ESTABLISH_CONNECTION , lastError ) ;
257
+ reject ( lastError || new InternalServerErrorException ( ERROR_MESSAGES . SERVER_CLOSED_CONNECTION ) ) ;
252
258
} ) ;
253
259
cluster . on ( 'ready' , ( ) : void => {
260
+ lastError = null ;
254
261
this . logger . log ( 'Successfully connected to the redis oss cluster.' ) ;
255
262
resolve ( cluster ) ;
256
263
} ) ;
264
+ cluster . on ( 'reconnecting' , ( ) : void => {
265
+ lastError = null ;
266
+ this . logger . log ( ERROR_MESSAGES . RECONNECTING_TO_DATABASE ) ;
267
+ } ) ;
257
268
} catch ( e ) {
258
269
reject ( e ) ;
259
270
}
@@ -275,19 +286,26 @@ export class RedisConnectionFactory {
275
286
276
287
return new Promise ( ( resolve , reject ) => {
277
288
try {
289
+ let lastError : Error ;
290
+
278
291
const client = new Redis ( config ) ;
279
292
client . on ( 'error' , ( e ) : void => {
280
293
this . logger . error ( 'Failed connection to the redis oss sentinel' , e ) ;
281
- reject ( e ) ;
294
+ lastError = e ;
282
295
} ) ;
283
296
client . on ( 'end' , ( ) : void => {
284
- this . logger . error ( ERROR_MESSAGES . SERVER_CLOSED_CONNECTION ) ;
285
- reject ( new InternalServerErrorException ( ERROR_MESSAGES . SERVER_CLOSED_CONNECTION ) ) ;
297
+ this . logger . error ( ERROR_MESSAGES . UNABLE_TO_ESTABLISH_CONNECTION , lastError ) ;
298
+ reject ( lastError || new InternalServerErrorException ( ERROR_MESSAGES . SERVER_CLOSED_CONNECTION ) ) ;
286
299
} ) ;
287
300
client . on ( 'ready' , ( ) : void => {
301
+ lastError = null ;
288
302
this . logger . log ( 'Successfully connected to the redis oss sentinel.' ) ;
289
303
resolve ( client ) ;
290
304
} ) ;
305
+ client . on ( 'reconnecting' , ( ) : void => {
306
+ lastError = null ;
307
+ this . logger . log ( ERROR_MESSAGES . RECONNECTING_TO_DATABASE ) ;
308
+ } ) ;
291
309
} catch ( e ) {
292
310
reject ( e ) ;
293
311
}
0 commit comments