Skip to content

Commit 3987ca4

Browse files
committed
refactor(retry): simplify telegram shouldRetry composition
1 parent 7b9a349 commit 3987ca4

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

‎src/infra/retry-policy.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ export const TELEGRAM_RETRY_DEFAULTS = {
2222
const TELEGRAM_RETRY_RE = /429|timeout|connect|reset|closed|unavailable|temporarily/i;
2323
const log = createSubsystemLogger("retry-policy");
2424

25+
function resolveTelegramShouldRetry(params: {
26+
shouldRetry?: (err: unknown) => boolean;
27+
strictShouldRetry?: boolean;
28+
}) {
29+
if (!params.shouldRetry) {
30+
return (err: unknown) => TELEGRAM_RETRY_RE.test(formatErrorMessage(err));
31+
}
32+
if (params.strictShouldRetry) {
33+
return params.shouldRetry;
34+
}
35+
return (err: unknown) =>
36+
params.shouldRetry?.(err) || TELEGRAM_RETRY_RE.test(formatErrorMessage(err));
37+
}
38+
2539
function getTelegramRetryAfterMs(err: unknown): number | undefined {
2640
if (!err || typeof err !== "object") {
2741
return undefined;
@@ -88,12 +102,7 @@ export function createTelegramRetryRunner(params: {
88102
...params.configRetry,
89103
...params.retry,
90104
});
91-
const shouldRetry = params.shouldRetry
92-
? params.strictShouldRetry
93-
? params.shouldRetry
94-
: (err: unknown) =>
95-
params.shouldRetry?.(err) || TELEGRAM_RETRY_RE.test(formatErrorMessage(err))
96-
: (err: unknown) => TELEGRAM_RETRY_RE.test(formatErrorMessage(err));
105+
const shouldRetry = resolveTelegramShouldRetry(params);
97106

98107
return <T>(fn: () => Promise<T>, label?: string) =>
99108
retryAsync(fn, {

0 commit comments

Comments
 (0)