Skip to content

fix(core): recognize PluginRouteError from a separate emdash instance - #3741

Merged
ascorbic merged 1 commit into
emdash-cms:mainfrom
KirbyBT:fix/plugin-route-error-module-copy
Oct 3, 2026
Merged

ascorbic merged 1 commit into
emdash-cms:mainfrom
KirbyBT:fix/plugin-route-error-module-copy

Conversation

@KirbyBT

@KirbyBT KirbyBT commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Under astro dev, a native plugin route that throws PluginRouteError (for example PluginRouteError.badRequest("…")) reaches the client as a generic 500 INTERNAL_ERROR instead of the error's code, status, and message.

Vite pre-bundles the plugin package for SSR with emdash kept external, so the plugin's PluginRouteError can come from a different module instance than the one PluginRouteHandler imports. The error instanceof PluginRouteError check in routes.ts then fails, and the error falls through to the unknown-error branch.

This adds isPluginRouteError() next to the class. It keeps the instanceof fast path and also accepts an Error named PluginRouteError with a string code and numeric status, so a route error from another copy of the module is still reported as the plugin intended. An ordinary error that only borrows the name is still treated as unknown.

#3602 also touches plugin route errors, but it changes how dispatchPluginApiRequest builds the response (passing details through), so the two changes are independent.

Closes #3588

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes (ran tsgo --noEmit in packages/core)
  • pnpm lint passes (ran type-aware oxlint --deny-warnings on the changed files)
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main. — n/a, no UI strings
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to an approved Discussion: https://github.com/emdash-cms/emdash/discussions/... — n/a, bug fix
  • I have included screenshots below if this PR changes the UI — n/a, no UI change

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Code (Claude Opus 5.5)

Screenshots / test output

Not applicable (no UI change).

The new regression test in packages/core/tests/unit/plugins/routes.test.ts throws a PluginRouteError-shaped error from a separate class, as a second module instance would:

  • without the fix: 1 failed | 52 passed (53) (the new test gets 500 INTERNAL_ERROR)
  • with the fix: 53 passed (53)

A second new test checks that an Error that only sets name = "PluginRouteError" still returns 500 INTERNAL_ERROR.

Under `astro dev`, Vite pre-bundles a plugin package for SSR with
`emdash` kept external, so the plugin's PluginRouteError can come from a
different module instance than the one the route dispatcher imports. The
`instanceof` check then fails, and a deliberate 4xx error from the plugin
reaches the client as a generic 500 INTERNAL_ERROR.

Match route errors by shape as well as by class.

Fixes emdash-cms#3588

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@changeset-bot

changeset-bot Bot commented Oct 2, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: eab932f

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

@github-actions github-actions Bot added area/core size/M review/needs-review No maintainer or bot review yet labels Oct 2, 2026
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

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

This is the right fix for the reported cross-instance PluginRouteError problem. Under astro dev, Vite can load the plugin's emdash import and the route handler's internal ./route-error.js as separate module instances, so instanceof identity fails and the error collapses to a generic 500.

The implementation is minimal and correct:

  • packages/core/src/plugins/route-error.ts adds isPluginRouteError(), keeping the fast instanceof path and falling back to a structural check (Error named "PluginRouteError" with a string code and numeric status).
  • packages/core/src/plugins/routes.ts replaces error instanceof PluginRouteError with isPluginRouteError(error), so a foreign copy still returns its intended code, status, message, and details.
  • The tests are real regression tests, not tautologies: one throws a separate PluginRouteError-shaped class and asserts the shaped error reaches the client; another confirms that an error that only borrows the name still becomes INTERNAL_ERROR.
  • The changeset is proportional and user-facing, naming the observable behavior and environment.

I also checked the tree for other instanceof PluginRouteError sites. The only remaining one is in packages/cloudflare/src/plugins/ai-search.ts, but the thrower and catcher there share the same imported PluginRouteError binding, so the cross-instance bug does not apply there. No AGENTS.md convention issues (no new UI strings, SQL, content-table queries, logged-out query paths, or comment violations).

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed review/needs-review No maintainer or bot review yet labels Oct 2, 2026
@KirbyBT

KirbyBT commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Oct 2, 2026

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

Thanks!

@ascorbic
ascorbic enabled auto-merge (squash) October 3, 2026 07:02
@pkg-pr-new

pkg-pr-new Bot commented Oct 3, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

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

@emdash-cms/auth

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

@emdash-cms/auth-atproto

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

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

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

emdash

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

create-emdash

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

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

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

@emdash-cms/plugin-cli

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

@emdash-cms/plugin-test

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

@emdash-cms/plugin-types

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

@emdash-cms/registry-client

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

@emdash-cms/registry-lexicons

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

@emdash-cms/registry-loader

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

@emdash-cms/registry-moderation

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

@emdash-cms/registry-verification

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

@emdash-cms/sandbox-workerd

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

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-field-kit

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

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: eab932f

@ascorbic
ascorbic merged commit 4bc129c into emdash-cms:main Oct 3, 2026
47 of 48 checks passed
@emdashbot

emdashbot Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Thanks for contributing to EmDash! 🎉

Join the EmDash Discord, then run /link to connect your GitHub account and claim the Contributor role.

@emdashbot emdashbot Bot mentioned this pull request Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

astro dev: PluginRouteError thrown from a native plugin route is returned as a generic INTERNAL_ERROR

2 participants