astro dev: PluginRouteError thrown from a native plugin route is returned as a generic INTERNAL_ERROR
Description
Under astro dev, a native plugin route handler that throws PluginRouteError (e.g. PluginRouteError.badRequest("…"), as the API routes docs suggest) returns a generic 500 instead of the error's code, status and message:
{ "error": { "code": "INTERNAL_ERROR", "message": "Plugin route error" } }The server log shows the thrown error was a PluginRouteError:
[plugin:<id>] Route handler failed: PluginRouteError: The Umami URL must start with https:// or http://.
at PluginRouteError.badRequest (/…/node_modules/emdash/…)Expected: 400 with { "code": "BAD_REQUEST", "message": "The Umami URL must start with https:// or http://." }, which the error instanceof PluginRouteError branch in the route dispatcher would produce.
Likely cause (not confirmed). In dev, Vite pre-bundles the plugin package for SSR (dependency optimized: <plugin package>, with an entry in node_modules/.vite/deps_ssr/_metadata.json). The pre-bundle keeps import … from "emdash" external, and that import appears to resolve to a different module instance of emdash than the one the host's route dispatcher uses. So the plugin's PluginRouteError class is not the dispatcher's, and the instanceof check fails. A duck-typed check (e.g. error?.name === "PluginRouteError" plus code and status), or excluding config plugins from SSR pre-bundling, would avoid it.
Workaround. Validate in the route's Zod input (reported correctly as VALIDATION_ERROR), or return failures as data.
Steps to reproduce
- In a native plugin:
routes: { boom: { handler: () => { throw PluginRouteError.badRequest("nope"); } } }, withPluginRouteErrorimported from"emdash". - Register it in
plugins: []and runastro dev. POST /_emdash/api/plugins/<id>/boomwith an admin token. The response is 500INTERNAL_ERROR"Plugin route error".
Environment
- emdash version: 1.0.1
- Astro 7.3.5,
@astrojs/cloudflare14, workerd dev - Node.js version: 26.7.0
- Runtime: Cloudflare Workers (astro dev). Production not tested.
- OS: Linux
Screenshots
Not applicable.
Source: emdash-cms/emdash