From 6660811053f6226667fd6d84a4fbb1ce58b4233b Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 21 Jan 2022 14:44:30 -0500 Subject: [PATCH 1/2] error if handle returns non-Response --- .changeset/green-mayflies-shave.md | 5 +++++ packages/kit/src/runtime/server/index.js | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .changeset/green-mayflies-shave.md diff --git a/.changeset/green-mayflies-shave.md b/.changeset/green-mayflies-shave.md new file mode 100644 index 000000000000..7d308c39b68e --- /dev/null +++ b/.changeset/green-mayflies-shave.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Error if handle hook returns something other than a Response diff --git a/packages/kit/src/runtime/server/index.js b/packages/kit/src/runtime/server/index.js index ff095219af26..89803cf8fceb 100644 --- a/packages/kit/src/runtime/server/index.js +++ b/packages/kit/src/runtime/server/index.js @@ -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); @@ -195,6 +195,13 @@ export async function respond(request, options, state = {}) { throw new Error('request in handle has been replaced with event' + details); } }); + + // TODO remove for 1.0 + 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); From 9fa1e7c45bf35f91cb82ae846601f13e911edd07 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 22 Jan 2022 07:50:05 -0500 Subject: [PATCH 2/2] change TODO comment --- packages/kit/src/runtime/server/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/runtime/server/index.js b/packages/kit/src/runtime/server/index.js index 89803cf8fceb..f0511349b096 100644 --- a/packages/kit/src/runtime/server/index.js +++ b/packages/kit/src/runtime/server/index.js @@ -196,7 +196,7 @@ export async function respond(request, options, state = {}) { } }); - // TODO remove for 1.0 + // 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); }