diff --git a/src/handlers/strategies/directoryListing.ts b/src/handlers/strategies/directoryListing.ts index 0cf4919..2bce657 100644 --- a/src/handlers/strategies/directoryListing.ts +++ b/src/handlers/strategies/directoryListing.ts @@ -152,8 +152,8 @@ async function fetchR2Result( cursor: string | undefined, env: Env ): Promise { - let retriesRemaining = R2_RETRY_LIMIT; - while (retriesRemaining > 0) { + let r2Error: unknown = undefined; + for (let i = 0; i < R2_RETRY_LIMIT; i++) { try { // Send request to R2 const result = await client.send( @@ -172,12 +172,12 @@ async function fetchR2Result( // Got an error, let's log it and retry console.error(`R2 ListObjectsV2 error: ${err}`); - retriesRemaining--; + r2Error = err; } } // R2 isn't having a good day, return a 500 - throw new Error(`R2 failed listing path ${bucketPath}`); + throw new Error(`R2 failed listing path ${bucketPath}: ${r2Error}`); } /** diff --git a/src/handlers/strategies/serveFile.ts b/src/handlers/strategies/serveFile.ts index 91835a3..e594084 100644 --- a/src/handlers/strategies/serveFile.ts +++ b/src/handlers/strategies/serveFile.ts @@ -56,17 +56,21 @@ async function r2GetWithRetries( key: string, options?: R2GetOptions ): Promise { + let r2Error: unknown = undefined; for (let i = 0; i < R2_RETRY_LIMIT; i++) { try { return await bucket.get(key, options); } catch (err) { // Log error & retry console.error(`R2 GetObject error: ${err}`); + r2Error = err; } } // R2 isn't having a good day, return a 500 & log to sentry - throw new Error(`R2 GetObject failed after ${R2_RETRY_LIMIT} retries: `); + throw new Error( + `R2 GetObject failed after ${R2_RETRY_LIMIT} retries: ${r2Error}` + ); } /** @@ -80,18 +84,21 @@ async function r2HeadWithRetries( bucket: R2Bucket, key: string ): Promise { + let r2Error: unknown = undefined; for (let i = 0; i < R2_RETRY_LIMIT; i++) { try { - const file = await bucket.head(key); - return file; + return await bucket.head(key); } catch (err) { // Log error & retry console.error(`R2 HeadObject error: ${err}`); + r2Error = err; } } // R2 isn't having a good day, return a 500 & log to sentry - throw new Error(`R2 HeadObject failed after ${R2_RETRY_LIMIT} retries: `); + throw new Error( + `R2 HeadObject failed after ${R2_RETRY_LIMIT} retries: ${r2Error}` + ); } /**