Skip to content

fix(templates): surface content query errors as 500 instead of 404 - #5013

Merged
swissky merged 4 commits into
emdash-cms:mainfrom
swissky:fix/templates-db-error-not-404
Nov 26, 2026
Merged

swissky merged 4 commits into
emdash-cms:mainfrom
swissky:fix/templates-db-error-not-404

Conversation

@swissky

@swissky swissky commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

getEmDashEntry() catches query errors and returns { entry: null, error }. Most shipped templates, demos and docs examples only checked entry, so when the database failed (for example D1_ERROR: D1 DB is overloaded during 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/ and infra/, plus the docs and building-emdash-site skill examples, to if (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:

Template code Error responses Still cached on refetch
main (error ignored) 104 × 404 0
return new Response(..., { status: 500 }) 368 × 500 115 (cache HIT, 24h TTL)
if (error) throw error; (this PR) 106 × 500 0 (every refetch returned 200)

Not in this PR: list pages using getEmDashCollection() also ignore error and 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 under templates/*/.agents/skills/ are already out of sync with skills/ and are left to the usual sync.

decodeSlug() now also returns undefined when the decoded slug contains a NUL byte. PostgreSQL rejects NUL in text parameters, so /posts/%00 failed the entry query; with the new throw error that 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 bypasses slugify(); such a URL now 404s as well. The docs examples this PR touches now decode their slug the same way, and the changeset covers the emdash change.

Type of change

  • Bug fix
  • Feature (requires an accepted feature plan or RFC)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Your connection to this change

  • I use EmDash and ran into the problem this PR addresses
  • I maintain a tool, dependency, or service that EmDash uses, and this change concerns that integration
  • A Maintainer approved this specific change before I opened the PR

Checklist

AI assistance disclosure

  • This PR includes AI-generated code or text — model/tool: Claude Opus 5.5

Screenshots / test output

Not applicable (no UI change).

packages/core/tests/repro/template-entry-error.render.test.ts renders the 18 template entry pages in the Astro container with getEmDashEntry returning 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 on main, the 18 error cases fail; with this PR all 21 pass.

@changeset-bot

changeset-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b3c7baf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/plugin-test Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
create-emdash Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

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

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Suggested change
<h1>{post?.data.title}</h1>
<h1>{post.data.title}</h1>

@pkg-pr-new

pkg-pr-new Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@5013

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@5013

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@5013

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@5013

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@5013

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@5013

emdash

npm i https://pkg.pr.new/emdash@5013

create-emdash

npm i https://pkg.pr.new/create-emdash@5013

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@5013

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@5013

@emdash-cms/plugin-test

npm i https://pkg.pr.new/@emdash-cms/plugin-test@5013

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@5013

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@5013

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@5013

@emdash-cms/registry-loader

npm i https://pkg.pr.new/@emdash-cms/registry-loader@5013

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/@emdash-cms/registry-moderation@5013

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@5013

upgrade-emdash

npm i https://pkg.pr.new/upgrade-emdash@5013

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@5013

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@5013

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@5013

@emdash-cms/plugin-ai-search

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-search@5013

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@5013

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@5013

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@5013

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@5013

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@5013

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@5013

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@5013

commit: b3c7baf

@swissky swissky added the bot:review Trigger an emdashbot code review on this PR label Nov 21, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@emdashbot emdashbot Bot removed the bot:review Trigger an emdashbot code review on this PR label Nov 21, 2026
@swissky
swissky force-pushed the fix/templates-db-error-not-404 branch from 64f7334 to 7ff901d Compare November 23, 2026 09:03

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 danielmlr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small nits, both fine as a follow-up:

  1. Postgres can't handle a NUL byte, so a URL like /posts/%00 now seems to give a 500 there instead of a 404, which shows up as an error in the logs. Maybe decodeSlug() could treat a slug with \u0000 as not found?

  2. The cheat sheet in skills/building-emdash-site/SKILL.md still skips error in its getEmDashEntry() example. Maybe add the same if (error) throw error; line there too?

@emdashbot emdashbot Bot added review/needs-review No maintainer or bot review yet and removed review/needs-review No maintainer or bot review yet labels Nov 23, 2026
@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-review No maintainer or bot review yet labels Nov 23, 2026
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.
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/approved Approved; no new commits since labels Nov 26, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@swissky
swissky merged commit c94f442 into emdash-cms:main Nov 26, 2026
60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants