Skip to content

Commit

Permalink
src: include error message when reporting failed R2 operations to sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Nov 18, 2023
1 parent 4498e8b commit 7f9ae9b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/handlers/strategies/directoryListing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ async function fetchR2Result(
cursor: string | undefined,
env: Env
): Promise<ListObjectsV2CommandOutput> {
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(
Expand All @@ -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}`);
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/handlers/strategies/serveFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,21 @@ async function r2GetWithRetries(
key: string,
options?: R2GetOptions
): Promise<R2Object | null> {
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}`
);
}

/**
Expand All @@ -80,18 +84,21 @@ async function r2HeadWithRetries(
bucket: R2Bucket,
key: string
): Promise<R2Object | null> {
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}`
);
}

/**
Expand Down

0 comments on commit 7f9ae9b

Please sign in to comment.