From 0bc3767d344f5c4affa63b7e39411c5e2291a9c7 Mon Sep 17 00:00:00 2001 From: priyagupta108 Date: Thu, 9 Jul 2026 09:40:20 +0530 Subject: [PATCH] Refactor error handling in isRateLimitError function for improved clarity --- __tests__/install-python.test.ts | 8 +------- dist/setup/index.js | 4 ++-- src/install-python.ts | 8 +++++--- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/__tests__/install-python.test.ts b/__tests__/install-python.test.ts index e1dbd060..860f168c 100644 --- a/__tests__/install-python.test.ts +++ b/__tests__/install-python.test.ts @@ -57,7 +57,6 @@ describe('getManifest', () => { it('should fall back to URL if repo returns a truncated/empty manifest', async () => { jest.useFakeTimers(); - // Simulate a truncated response that parses to an empty array. (tc.getManifestFromRepo as jest.Mock).mockResolvedValue([]); (httpm.HttpClient.prototype.getJson as jest.Mock).mockResolvedValue({ result: mockManifest @@ -71,9 +70,7 @@ describe('getManifest', () => { it('should retry on a transient invalid manifest and then succeed', async () => { jest.useFakeTimers(); (tc.getManifestFromRepo as jest.Mock) - // First attempt returns a truncated/empty body. .mockResolvedValueOnce([]) - // Retry returns a valid manifest. .mockResolvedValueOnce(mockManifest); const promise = getManifest(); await jest.runAllTimersAsync(); @@ -84,13 +81,12 @@ describe('getManifest', () => { it('should fail loudly when the manifest is truncated/empty on every source', async () => { jest.useFakeTimers(); - // Both the API and the raw URL keep returning truncated/empty bodies. (tc.getManifestFromRepo as jest.Mock).mockResolvedValue([]); (httpm.HttpClient.prototype.getJson as jest.Mock).mockResolvedValue({ result: [] }); const promise = getManifest(); - // Attach a rejection handler before advancing timers to avoid unhandled rejection warnings. + // Prevent unhandled rejection before timers advance. const catchPromise = promise.catch(() => {}); await jest.runAllTimersAsync(); await catchPromise; @@ -107,8 +103,6 @@ describe('getManifest', () => { (httpm.HttpClient.prototype.getJson as jest.Mock).mockResolvedValue({ result: mockManifest }); - // No fake timers needed: a rate-limit error must short-circuit retries - // (and their backoff) and fall straight through to the URL fallback. const manifest = await getManifest(); expect(manifest).toEqual(mockManifest); expect(tc.getManifestFromRepo as jest.Mock).toHaveBeenCalledTimes(1); diff --git a/dist/setup/index.js b/dist/setup/index.js index 31cd4ff7..e0982e71 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -55725,8 +55725,8 @@ function sleep(ms) { } // HTTP 403/429 from http-client (`statusCode`) or tool-cache (`httpStatusCode`). function isRateLimitError(err) { - const status = err.httpStatusCode ?? - err.statusCode; + const e = err; + const status = e?.httpStatusCode ?? e?.statusCode; return status === 403 || status === 429; } // Fetches and validates a manifest, retrying transient failures with backoff. diff --git a/src/install-python.ts b/src/install-python.ts index d2dd79b5..35b3170e 100644 --- a/src/install-python.ts +++ b/src/install-python.ts @@ -151,9 +151,11 @@ function sleep(ms: number): Promise { // HTTP 403/429 from http-client (`statusCode`) or tool-cache (`httpStatusCode`). function isRateLimitError(err: unknown): boolean { - const status = - (err as {httpStatusCode?: number}).httpStatusCode ?? - (err as {statusCode?: number}).statusCode; + const e = err as + | {httpStatusCode?: number; statusCode?: number} + | null + | undefined; + const status = e?.httpStatusCode ?? e?.statusCode; return status === 403 || status === 429; }