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. 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