0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

qol(routing): improve endpoint error message (#10072)

* Clarify error messages in endpoint routing.

* add changeset
This commit is contained in:
Arsh 2024-02-12 08:25:46 -07:00 committed by GitHub
parent 7371e1b5fc
commit 8106178043
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Clarifies error messages in endpoint routing.

View file

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