Skip to content

Commit

Permalink
src: fix range errors
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Nov 6, 2024
1 parent 16ede56 commit b0b37ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/middleware/r2Middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CACHE_HEADERS } from '../constants/cache';
import type { Context } from '../context';
import type { GetFileResult } from '../providers/provider';
import { R2Provider } from '../providers/r2Provider';
import responses from '../responses';
import { hasTrailingSlash, isDirectoryPath } from '../utils/path';
Expand Down Expand Up @@ -107,9 +108,22 @@ async function getFile(
r2Path: string,
ctx: Context
): Promise<Response> {
const result = await getProvider(ctx).getFile(r2Path, {
conditionalHeaders: parseConditionalHeaders(request.headers),
});
const provider = getProvider(ctx);

let result: GetFileResult | undefined;
try {
result = await provider.getFile(r2Path, {
conditionalHeaders: parseConditionalHeaders(request.headers),
});
} catch (err) {
// Check if R2 threw a range not compatible error
if (err instanceof Error && err.message.includes('10039')) {
return new Response(undefined, { status: 416 });
}

ctx.sentry.captureException(err)
throw err;
}

if (result === undefined) {
return responses.fileNotFound(request.method);
Expand Down
7 changes: 6 additions & 1 deletion src/providers/r2Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class R2Provider implements Provider {
};
}

/**
* Important: the caller is responsible for wrapping this in a try/catch
* and reporting any errors to Sentry
*/
async getFile(
path: string,
options?: GetFileOptions
Expand All @@ -59,7 +63,8 @@ export class R2Provider implements Provider {
});
},
R2_RETRY_LIMIT,
this.ctx.sentry
// Don't pass sentry in,
undefined
);

if (object === null) {
Expand Down

0 comments on commit b0b37ee

Please sign in to comment.