From 8106178043050d142bf385bed2990730518f28e2 Mon Sep 17 00:00:00 2001 From: Arsh <69170106+lilnasy@users.noreply.github.com> Date: Mon, 12 Feb 2024 08:25:46 -0700 Subject: [PATCH] qol(routing): improve endpoint error message (#10072) * Clarify error messages in endpoint routing. * add changeset --- .changeset/sweet-chicken-sneeze.md | 5 +++++ packages/astro/src/runtime/server/endpoint.ts | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) 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 0000000000..9f145d3265 --- /dev/null +++ b/.changeset/sweet-chicken-sneeze.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Clarifies error messages in endpoint routing. diff --git a/packages/astro/src/runtime/server/endpoint.ts b/packages/astro/src/runtime/server/endpoint.ts index 49f00224e6..66454fb69d 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