Repository navigation
src: handle r2 errors in R2Middleware #1071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
flakey5
wants to merge
1
commit into
main
Choose a base branch
from
flakey5/1069
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+52
−19
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,11 @@ import type { Middleware } from './middleware'; | |
| import latestVersions from '../constants/latestVersions.json' assert { type: 'json' }; | ||
| import type { Request } from '../routes/request'; | ||
| import { renderDirectoryListing } from '../utils/directoryListing'; | ||
| import { env } from 'cloudflare:workers'; | ||
| import { getOriginalUrl, parseConditionalHeaders } from '../utils/request'; | ||
| import { once } from '../utils/memo'; | ||
| import internalServerError from '../responses/internalServerError'; | ||
| import type { Env } from '../env'; | ||
|
|
||
| const getProvider = once((ctx: Context) => new R2Provider({ ctx })); | ||
|
|
||
|
|
@@ -35,6 +38,44 @@ export class R2Middleware implements Middleware { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Wrap an R2 call with common error handling | ||
| * @param fn | ||
| * @returns <T> if fn didn't throw, a {@link Reponse} otherwise | ||
| * @throws anything that's thrown by fn and isn't an error | ||
| */ | ||
| async function wrapR2Call<T>(fn: () => Promise<T>): Promise<T | Response> { | ||
| try { | ||
| const result = await fn(); | ||
|
|
||
| return result; | ||
| } catch (err) { | ||
| if (err instanceof Error) { | ||
| // Error object likely from R2, let's handle it here | ||
|
|
||
| if (err.message.includes('10020')) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these error codes be in a constant with |
||
| // R2 error: object name not valid, url probably has some weirdness in it | ||
| return responses.badRequest(); | ||
| } else if (err.message.includes('10039')) { | ||
| // R2 error: range not compatible, probably out of bounds | ||
| return new Response(undefined, { | ||
| status: 416, | ||
| headers: { 'cache-control': CACHE_HEADERS.failure }, | ||
| }); | ||
| } | ||
|
|
||
| // We need to report it ourselves since we're not throwing it to the | ||
| // router | ||
| Sentry.captureException(err); | ||
|
|
||
| return internalServerError(err, env as Env); | ||
| } | ||
|
|
||
| // Not an error, throw just for visibility | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| async function handleDirectory( | ||
| request: Request, | ||
| r2Path: string, | ||
|
|
@@ -110,7 +151,11 @@ async function headFile( | |
| r2Path: string, | ||
| ctx: Context | ||
| ): Promise<Response> { | ||
| const result = await getProvider(ctx).headFile(r2Path); | ||
| const result = await wrapR2Call(() => getProvider(ctx).headFile(r2Path)); | ||
|
|
||
| if (result instanceof Response) { | ||
| return result; | ||
| } | ||
|
|
||
| if (result === undefined) { | ||
| return responses.fileNotFound(request.method); | ||
|
|
@@ -129,26 +174,14 @@ async function getFile( | |
| ): Promise<Response> { | ||
| const provider = getProvider(ctx); | ||
|
|
||
| let result: GetFileResult | undefined; | ||
| try { | ||
| result = await provider.getFile(r2Path, { | ||
| const result = await wrapR2Call(() => | ||
| provider.getFile(r2Path, { | ||
| conditionalHeaders: parseConditionalHeaders(request.headers), | ||
| }); | ||
| } catch (err) { | ||
| if (err instanceof Error) { | ||
| if (err.message.includes('10020')) { | ||
| // Object name not valid, url probably has some weirdness in it | ||
| return responses.badRequest(); | ||
| } else if (err.message.includes('10039')) { | ||
| // Range not compatible, probably out of bounds | ||
| return new Response(undefined, { | ||
| status: 416, | ||
| headers: { 'cache-control': CACHE_HEADERS.failure }, | ||
| }); | ||
| } | ||
| } | ||
| }) | ||
| ); | ||
|
|
||
| throw err; | ||
| if (result instanceof Response) { | ||
| return result; | ||
| } | ||
|
|
||
| if (result === undefined) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could the method name be maybe more descriptive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also could the type not be a generic T, maybe extend something that is an actual R2 call/method signature?