diff --git a/lib/utils/handleRetry.ts b/lib/utils/handleRetry.ts index 90dac99..d2572c8 100644 --- a/lib/utils/handleRetry.ts +++ b/lib/utils/handleRetry.ts @@ -1,5 +1,7 @@ import { of, throwError } from "rxjs"; -import { mergeMap, retryWhen, take, delay } from "rxjs/operators"; +import { mergeMap, retryWhen, delay } from "rxjs/operators"; +import { LOGGER } from "./consts"; +import { Logger } from "@nestjs/common"; export function handleRetry( retryAttempts = 9, @@ -7,23 +9,33 @@ export function handleRetry( verbose = false, toRetry: (err: any) => boolean = (_err: any) => true, ) { + const logger = new Logger(LOGGER); + return (source: import("rxjs").Observable) => source.pipe( retryWhen((attempts) => attempts.pipe( - take(retryAttempts), mergeMap((error, index) => { const includeError = toRetry(error); - if (verbose && includeError) { - console.warn( - `Attempt ${index + 1}: Retrying in ${ - retryDelay / 1000 - } seconds...`, - ); + + if (includeError) { + if (verbose) { + logger.warn( + `Attempt ${index + 1}: Retrying in ${ + retryDelay / 1000 + } seconds...`, + ); + } + + if (index + 1 >= retryAttempts) { + return throwError(() => new Error(error.message)); + } + + return of(error).pipe(delay(retryDelay)); } - return includeError ? of(error) : throwError(error); + + return throwError(() => error); }), - delay(retryDelay), ), ), );