Skip to content

Commit

Permalink
Fix issue where 404/500 status codes were logged as "[200]" (#9336)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott authored Dec 6, 2023
1 parent 0bb3d53 commit c769010
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-mice-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

dev: fix issue where 404 and 500 responses were logged as 200
2 changes: 1 addition & 1 deletion packages/astro/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}]`) +
` ` +
Expand Down
33 changes: 16 additions & 17 deletions packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export async function handleRoute({
req({
url: pathname,
method: incomingRequest.method,
statusCode: response.status,
statusCode: status ?? response.status,
reqTime: timeEnd - timeStart,
})
);
Expand All @@ -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 });
}
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 {
Expand Down

0 comments on commit c769010

Please sign in to comment.