Skip to content

fix(adapter-cloudflare-workers): throws status 404 if KVError instanceof #9509

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/cool-foxes-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
'@sveltejs/adapter-cloudflare-workers': minor
---
return 404 for non-existent asset rather than throwing KVError
39 changes: 27 additions & 12 deletions packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Server } from 'SERVER';
import { manifest, prerendered } from 'MANIFEST';
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler';
import {
getAssetFromKV,
mapRequestToAsset,
MethodNotAllowedError,
NotFoundError
} from '@cloudflare/kv-asset-handler';
import static_asset_manifest_json from '__STATIC_CONTENT_MANIFEST';
const static_asset_manifest = JSON.parse(static_asset_manifest_json);

Expand Down Expand Up @@ -86,19 +91,29 @@ export default {
* @param {any} context
*/
async function get_asset_from_kv(req, env, context, map = mapRequestToAsset) {
return await getAssetFromKV(
{
request: req,
waitUntil(promise) {
return context.waitUntil(promise);
try {
return await getAssetFromKV(
{
request: req,
waitUntil(promise) {
return context.waitUntil(promise);
}
},
{
ASSET_NAMESPACE: env.__STATIC_CONTENT,
ASSET_MANIFEST: static_asset_manifest,
mapRequestToAsset: map
}
},
{
ASSET_NAMESPACE: env.__STATIC_CONTENT,
ASSET_MANIFEST: static_asset_manifest,
mapRequestToAsset: map
);
} catch (e) {
if (e instanceof NotFoundError) {
return new Response('Not found', { status: 404 });
} else if (e instanceof MethodNotAllowedError) {
return new Response('Method not allowed', { status: 405 });
} else {
return new Response('Internal Error', { status: 500 });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it feels like this might make it hard for people to figure out what the underlying error is

Copy link
Contributor Author

@vicentematus vicentematus Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cant see how to improve this out. Should it throw the error that it catches on the try catch clause🤔 ?

}
);
}
}

/**
Expand Down