From c217ada1a6eb6df2c067b129fe68deffd3b92e00 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Fri, 9 Feb 2024 15:41:04 +0000 Subject: [PATCH 1/2] Clarify error messages in endpoint routing. --- packages/astro/src/runtime/server/endpoint.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/runtime/server/endpoint.ts b/packages/astro/src/runtime/server/endpoint.ts index 49f00224e61d..66454fb69d9e 100644 --- a/packages/astro/src/runtime/server/endpoint.ts +++ b/packages/astro/src/runtime/server/endpoint.ts @@ -23,10 +23,10 @@ export async function renderEndpoint( )} requests are not available for a static site. Update your config to \`output: 'server'\` or \`output: 'hybrid'\` to enable.` ); } - if (typeof handler !== 'function') { + if (handler === undefined) { logger.warn( 'router', - `No API Route handler exists for the method "${method}" for the route ${url.pathname}.\n` + + `No API Route handler exists for the method "${method}" for the route "${url.pathname}".\n` + `Found handlers: ${Object.keys(mod) .map((exp) => JSON.stringify(exp)) .join(', ')}\n` + @@ -38,6 +38,13 @@ export async function renderEndpoint( // 404. Should be handled by 404.astro route if possible. return new Response(null, { status: 404 }); } + if (typeof handler !== "function") { + logger.error( + 'router', + `The route "${url.pathname}" exports a value for the method "${method}", but it is of the type ${typeof handler} instead of a function.` + ); + return new Response(null, { status: 500 }); + } const response = await handler.call(mod, context); // Endpoints explicitly returning 404 or 500 response status should From 79b1d16f1b75bae961f2e0db4d21888ef932ce25 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Fri, 9 Feb 2024 15:46:05 +0000 Subject: [PATCH 2/2] add changeset --- .changeset/sweet-chicken-sneeze.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sweet-chicken-sneeze.md diff --git a/.changeset/sweet-chicken-sneeze.md b/.changeset/sweet-chicken-sneeze.md new file mode 100644 index 000000000000..9f145d3265db --- /dev/null +++ b/.changeset/sweet-chicken-sneeze.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Clarifies error messages in endpoint routing.