Skip to content

Commit

Permalink
error if handle returns non-Response (#3496)
Browse files Browse the repository at this point in the history
* error if handle returns non-Response

* change TODO comment
  • Loading branch information
Rich-Harris authored Jan 22, 2022
1 parent 1873156 commit a3817ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/green-mayflies-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Error if handle hook returns something other than a Response
9 changes: 8 additions & 1 deletion packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export async function respond(request, options, state = {}) {
let ssr = true;

try {
return await options.hooks.handle({
const response = await options.hooks.handle({
event,
resolve: async (event, opts) => {
if (opts && 'ssr' in opts) ssr = /** @type {boolean} */ (opts.ssr);
Expand Down Expand Up @@ -195,6 +195,13 @@ export async function respond(request, options, state = {}) {
throw new Error('request in handle has been replaced with event' + details);
}
});

// TODO for 1.0, change the error message to point to docs rather than PR
if (response && !(response instanceof Response)) {
throw new Error('handle must return a Response object' + details);
}

return response;
} catch (/** @type {unknown} */ e) {
const error = coalesce_to_error(e);

Expand Down

0 comments on commit a3817ab

Please sign in to comment.