question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error thrown when returning endpoint Response without body

See original GitHub issue

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/kit@1.0.0-next.100, svelte@3.38.2, vite@2.2.4.

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
tctree333commented, May 6, 2021

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!

0reactions
traedcommented, 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

NextJs API endpoint returning response object without ' ...
On the client side, I would like to get access to the 'message' inside the body from the response object that is returned...
Read more >
Should "No Results" be an error in a RESTful response?
Summary: If the client expects any number of results, from zero to large numbers, then return "success" if all results are delivered, even...
Read more >
Best Practices for REST API Error Handling
The simplest way we handle errors is to respond with an appropriate status code. Here are some common response codes: 400 Bad Request...
Read more >
API HTTP status codes reference
An HTTP 400 error will be returned if there is incorrect content in the request body sent to the service. endpoint. HTTP 409...
Read more >
Handle errors in ASP.NET Core web APIs
Using the preceding code, when an API controller returns BadRequest , an HTTP 400 response status is returned with no response body.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found