0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Fix issue where 404/500 status codes were logged as "[200]" (#9336)

This commit is contained in:
Fred K. Schott 2023-12-06 04:07:52 -08:00 committed by GitHub
parent 0bb3d53221
commit c769010655
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 19 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
dev: fix issue where 404 and 500 responses were logged as 200

View file

@ -37,7 +37,7 @@ export function req({
method?: string;
reqTime?: number;
}): string {
const color = statusCode >= 400 ? red : statusCode >= 300 ? yellow : blue;
const color = statusCode >= 500 ? red : statusCode >= 300 ? yellow : blue;
return (
color(`[${statusCode}]`) +
` ` +

View file

@ -332,7 +332,7 @@ export async function handleRoute({
req({
url: pathname,
method: incomingRequest.method,
statusCode: response.status,
statusCode: status ?? response.status,
reqTime: timeEnd - timeStart,
})
);
@ -356,24 +356,23 @@ export async function handleRoute({
}
if (route.type === 'endpoint') {
await writeWebResponse(incomingResponse, response);
} else {
if (
// We are in a recursion, and it's possible that this function is called itself with a status code
// By default, the status code passed via parameters is computed by the matched route.
//
// By default, we should give priority to the status code passed, although it's possible that
// the `Response` emitted by the user is a redirect. If so, then return the returned response.
response.status < 400 &&
response.status >= 300
) {
await writeSSRResult(request, response, incomingResponse);
return;
} else if (status && response.status !== status && (status === 404 || status === 500)) {
// Response.status is read-only, so a clone is required to override
response = new Response(response.body, { ...response, status });
}
await writeSSRResult(request, response, incomingResponse);
return;
}
// We are in a recursion, and it's possible that this function is called itself with a status code
// By default, the status code passed via parameters is computed by the matched route.
//
// By default, we should give priority to the status code passed, although it's possible that
// the `Response` emitted by the user is a redirect. If so, then return the returned response.
if (response.status < 400 && response.status >= 300) {
await writeSSRResult(request, response, incomingResponse);
return;
}
// Apply the `status` override to the response object before responding.
// Response.status is read-only, so a clone is required to override.
if (status && response.status !== status && (status === 404 || status === 500)) {
response = new Response(response.body, { ...response, status });
}
await writeSSRResult(request, response, incomingResponse);
}
interface GetScriptsAndStylesParams {