Skip to content
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

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

Conversation

vicentematus
Copy link
Contributor

@vicentematus vicentematus commented Mar 25, 2023

Describe the problem

Fixes #9288. The bug occurs when deploying to Cloudflare Workers, where requests for non-existing assets result in a 500 internal error instead of a 404 error.

The Solution

Add a try-catch clause to handle the exception if the error is an instance of KVError from Cloudflare Workers it will change the error.status to 404 instead of the default 500 as described in the PR.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@changeset-bot
Copy link

changeset-bot bot commented Mar 25, 2023

🦋 Changeset detected

Latest commit: 8907ece

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/adapter-cloudflare-workers Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris
Copy link
Member

Thanks for the PR. It's not totally clear to me what effect setting error.status = 404 is supposed to have — judging by https://github.com/cloudflare/kv-asset-handler#getassetfromkv we should be doing this instead:

async function get_asset_from_kv(req, env, context, map = mapRequestToAsset) {
	try {
		return await getAssetFromKV(
			{
				request: req,
				waitUntil(promise) {
					return context.waitUntil(promise);
				}
			},
			{
				ASSET_NAMESPACE: env.__STATIC_CONTENT,
				ASSET_MANIFEST: static_asset_manifest,
				mapRequestToAsset: map
			}
		);
	} catch (err) {
-		if (err instanceof KVError) err.status = 404;
+		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 })
+		}

-		throw err;
	}
}

The code in this PR is somewhat simpler, but is that behaviour documented somewhere?

@vicentematus
Copy link
Contributor Author

vicentematus commented Apr 24, 2023

My bad, i was confused. Rich is right. I added the errors that are documented on the documentation of kv-asset-handler. Fixed.

} 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🤔 ?

@eltigerchino
Copy link
Member

eltigerchino commented Dec 20, 2023

closing this since the issue has been resolved. Completed by #4276

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

adapter-cloudflare-workers: Throws KVError if asked for a nonexisting asset
4 participants