fix(plugin-ui-static): unwrap DrizzleQueryError when checking for 22P02

The plugin static asset route tries to look up a plugin by UUID first,
catching Postgres error 22P02 (invalid_text_representation) to fall back
to a key-based lookup. However, drizzle-orm wraps all query errors in a
DrizzleQueryError that stores the original error in `.cause` — so
`error.code` is always undefined and the fallback to `getByKey()` never
fires.

Fix by walking the error chain (top-level `code`, then `cause.code`,
recursively) so plugins that embed their key (e.g. `agent-pixels.camera`)
in `/_plugins/:pluginId/` asset URLs are resolved correctly and no longer
return 500.
This commit is contained in:
Alkim Ake Gozen 2026-06-07 23:40:59 +09:00
parent 384903bdf4
commit 3cfadd3326
Signed by: ake
GPG key ID: 130166E392FFAFDD

View file

@ -246,10 +246,16 @@ export function pluginUiStaticRoutes(db: Db, options: PluginUiStaticRouteOptions
try {
plugin = await registry.getById(pluginId);
} catch (error) {
const maybeCode =
typeof error === "object" && error !== null && "code" in error
? (error as { code?: unknown }).code
: undefined;
// DrizzleQueryError wraps the original Postgres error in `.cause`, so
// `error.code` is undefined — check both the top-level code and the
// nested cause to correctly identify the invalid-UUID error (22P02).
const getCode = (e: unknown): unknown => {
if (typeof e !== "object" || e === null) return undefined;
if ("code" in e) return (e as { code?: unknown }).code;
if ("cause" in e) return getCode((e as { cause?: unknown }).cause);
return undefined;
};
const maybeCode = getCode(error);
if (maybeCode !== "22P02") {
throw error;
}