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

Error thrown when returning endpoint Response without body #1351

Closed
tctree333 opened this issue May 6, 2021 · 5 comments · Fixed by #1808
Closed

Error thrown when returning endpoint Response without body #1351

tctree333 opened this issue May 6, 2021 · 5 comments · Fixed by #1808
Labels
bug Something isn't working
Milestone

Comments

@tctree333
Copy link

Describe the bug
If an endpoint returns an object without the body key specified, an error is thrown. This only seems to happen with a 200 status code.

// src/routes/example.json.js

export function get() {
	// does not work
	return {};
	// also does not work
	return { status: 200 };
	// works
	return { body: {} };
	// also works
	return { status: 404 };
}

Stack trace below.

Expected behavior
No error thrown, or at least a more descriptive one.

Stacktraces

Stack trace
Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at hash (file:///[project_path]/node_modules/@sveltejs/kit/dist/ssr.js:1518:11)
    at render (file:///[project_path]/node_modules/@sveltejs/kit/dist/ssr.js:1483:26)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.handle (/[project_path]/src/hooks.ts:18:14)
    at async respond (file:///[project_path]/node_modules/@sveltejs/kit/dist/ssr.js:1450:10)
    at async Immediate.<anonymous> (file:///[project_path]/node_modules/@sveltejs/kit/dist/chunks/index.js:3304:23)

Information about your SvelteKit Installation:

Tested on @sveltejs/[email protected], [email protected], [email protected].

Severity
This isn't too severe since it's very easy to return an empty object as the body, but the error message doesn't really help.

Additional context
Seems like it's trying to hash the body for the ETag header, but body is undefined.

@benmccann benmccann added the bug Something isn't working label May 6, 2021
@benmccann benmccann added this to the 1.0 milestone May 6, 2021
@Rich-Harris
Copy link
Member

Arguably we should provide a more informative error, but this is an error, no? When would a 200 GET response not have a body?

@Mlocik97
Copy link
Contributor

Mlocik97 commented May 6, 2021

I agree with Rich,... btw no body and empty body is not same thing.

@tctree333
Copy link
Author

A more descriptive error message would be great since I ran into it a couple times and it wasn't clear what was going on. I do agree that it doesn't entirely make sense for there to be no body but when I'm just testing if something works sometimes I forget to add body stuff, and then the error makes it seem like the thing I'm testing doesn't work!

@jerrythomas
Copy link

I ran into the same issue today. The error message was "Cannot read property 'length' of undefined".

My mistake was that I copied this snippet which did not include the body in the response and didn't think while copying.

https://kit.svelte.dev/docs#ssr-and-javascript-ssr
To set multiple cookies in a single set of response headers, you can return an array:

return {
	headers: {
		'set-cookie': [cookie1, cookie2]
	}
};

@traed
Copy link

traed commented Jul 2, 2021

Arguably we should provide a more informative error, but this is an error, no? When would a 200 GET response not have a body?

I get this on a 200 DELETE though and in that case a body doesn't make sense, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants