Error thrown when returning endpoint Response without body
See original GitHub issueDescribe 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:
- Created 2 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
I get this on a 200 DELETE though and in that case a body doesn’t make sense, right?