Repository navigation
fix(templates): surface content query errors as 500 instead of 404 - #5013
Conversation
🦋 Changeset detectedLatest commit: b3c7baf The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
The approach is sound: rethrowing the error from getEmDashEntry() before the !entry check lets query failures reach Astro's error pipeline. Previously they either became a 404 or a returned 500 Response that could pick up the route cache rule's TTL. Both reviewers approved and found no bugs. One reviewer traced the redirect middleware, where await next() has no surrounding catch, and checked every entry-page call site, the new regression test and the changeset exemption. The other reviewer reached the same conclusions. The only comment is a small docs cleanup. Neither reviewer ran the checks or reproduced the Cloudflare cache measurements. Collection-list pages that drop error are noted as out of scope and a sensible follow-up.
Reviewed by anthropic/claude-opus-5-5 and openai/gpt-6.1-sol.
| const author = post.references?.author.entries[0]; | ||
| --- | ||
|
|
||
| <h1>{post?.data.title}</h1> |
There was a problem hiding this comment.
[suggestion] The new guard on line 116 (if (!post) return Astro.rewrite("/404");) means post can't be null here, and line 118 already drops the optional chain. If you drop it here as well, the example stays consistent and readers won't assume post might still be null at this point.
| <h1>{post?.data.title}</h1> | |
| <h1>{post.data.title}</h1> |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-test
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-loader
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
upgrade-emdash
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-ai-search
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
The approach is sound. Each entry page now throws the error returned by getEmDashEntry() before the !entry check. Query failures therefore reach Astro's error pipeline. Before, they became a 404 or a returned 500 Response that could pick up the route cache rule's TTL. Both reviewers approved and raised no findings. The earlier docs suggestion in relations.mdx (dropping the optional chain on post.data.title) is resolved at this head. Both reviewers did static review only. Neither ran the checks or reproduced the Cloudflare cache measurements. Collection-list pages that still ignore error from getEmDashCollection() are out of scope and a reasonable follow-up.
Reviewed by anthropic/claude-opus-5-5 and openai/gpt-6.1-sol.
64f7334 to
7ff901d
Compare
There was a problem hiding this comment.
The approach remains sound: throwing content-query errors before handling missing entries keeps operational failures distinct from genuine 404s and delegates them to Astro's error pipeline.
The earlier relations.mdx suggestion is resolved: the example now uses post.data.title after the not-found guard. I checked the follow-up against the current PR diff and found no new issues in the PR's changes. No findings remain open.
Static review only; I did not run tests, builds, or reproduce the Cloudflare cache measurements.
Follow-up review by openai/gpt-6.1-sol, scoped to the changes since the last review.
danielmlr
left a comment
There was a problem hiding this comment.
Two small nits, both fine as a follow-up:
-
Postgres can't handle a NUL byte, so a URL like
/posts/%00now seems to give a 500 there instead of a 404, which shows up as an error in the logs. MaybedecodeSlug()could treat a slug with\u0000as not found? -
The cheat sheet in
skills/building-emdash-site/SKILL.mdstill skipserrorin itsgetEmDashEntry()example. Maybe add the sameif (error) throw error;line there too?
PostgreSQL rejects NUL bytes in text parameters, so /posts/%00 failed the entry query. Now that templates throw query errors, that became a 500 instead of a 404. Also adds the error check to the skill cheat sheet.
There was a problem hiding this comment.
The approach remains sound: throwing query errors distinguishes operational failures from missing content, and rejecting NUL-containing slugs before querying avoids turning invalid URLs into database errors.
I checked the follow-up’s slug-decoding change, regression tests, changeset, and updated documentation and skill examples. The earlier relations.mdx suggestion remains resolved. I found no new issues in the PR-specific changes; no findings remain open.
Static review only; I did not run tests or reproduce the Cloudflare cache measurements.
Follow-up review by openai/gpt-6.1-sol, scoped to the changes since the last review.
What does this PR do?
getEmDashEntry()catches query errors and returns{ entry: null, error }. Most shipped templates, demos and docs examples only checkedentry, so when the database failed (for exampleD1_ERROR: D1 DB is overloadedduring a traffic burst) a valid URL answered 404. Crawlers take that as "this page is gone".The portfolio templates and a few docs examples did handle the error, but by returning
new Response(..., { status: 500 }). On a route with a route cache rule, Astro attaches the rule's cache headers to that returned response, so the 500 gets edge cached for the full TTL.This PR changes every entry page in
templates/,demos/,e2e/,fixtures/andinfra/, plus the docs andbuilding-emdash-siteskill examples, toif (error) throw error;before the 404 check. A thrown error is rendered by Astro outside the route cache handler, so the 500 carries no cache headers and the next request renders again.I measured this on a Cloudflare Worker with D1 and route caching, sending 3,042 requests from 6 regions in a burst that overloads D1:
main(error ignored)return new Response(..., { status: 500 })if (error) throw error;(this PR)Not in this PR: list pages using
getEmDashCollection()also ignoreerrorand render an empty 200. The portfolio/work?cursor=…page keeps its existing behavior of answering any query error with a 404 when a cursor is set, because the query API can't yet tell a malformed cursor from a database error. The template-local copies of the skill undertemplates/*/.agents/skills/are already out of sync withskills/and are left to the usual sync.decodeSlug()now also returnsundefinedwhen the decoded slug contains a NUL byte. PostgreSQL rejects NUL in text parameters, so/posts/%00failed the entry query; with the newthrow errorthat would have become a 500 instead of the earlier 404. Routes that decode their slug with it now 404 before querying. On SQLite a slug containing a NUL byte could only come from an API write that bypassesslugify(); such a URL now 404s as well. The docs examples this PR touches now decode their slug the same way, and the changeset covers theemdashchange.Type of change
Your connection to this change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.AI assistance disclosure
Screenshots / test output
Not applicable (no UI change).
packages/core/tests/repro/template-entry-error.render.test.tsrenders the 18 template entry pages in the Astro container withgetEmDashEntryreturning an error and expects the render to reject with that error. Three of them are also rendered with a missing entry and no error, and must answer 404. Against the templates onmain, the 18 error cases fail; with this PR all 21 pass.