From d220add4e548fb6c7fb74f020e25410e49515af1 Mon Sep 17 00:00:00 2001 From: taoky Date: Fri, 29 Sep 2023 01:41:21 +0800 Subject: [PATCH] winget-source: retry also when status >= 400 (#103) Fix https://github.com/ustclug/discussions/issues/435. --- winget-source/utilities.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/winget-source/utilities.js b/winget-source/utilities.js index 26455a1..4843765 100644 --- a/winget-source/utilities.js +++ b/winget-source/utilities.js @@ -14,9 +14,18 @@ import { EX_IOERR, EX_SOFTWARE, EX_USAGE } from './sysexits.js' /** * `fetch` implementation with retry support. * - * Defaults to 3 retries with 1000ms delay, on network errors only. + * 3 retries with 1000ms delay, on network errors and HTTP code >= 400. */ -const fetch = withRetry(originalFetch); +const fetch = withRetry(originalFetch, { + retryOn: (attempt, error, response) => { + if (attempt > 3) return false; + + if (error || response.status >= 400) { + winston.warn(`retrying ${response.url} (${attempt})`); + return true; + } + } +}); /** The remote URL of a pre-indexed WinGet source repository. */ const remote = process.env.WINGET_REPO_URL ?? 'https://cdn.winget.microsoft.com/cache';