Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat: enhance error handling for NeonDbError to classify connection-level transport failures as db-unavailable; add tests for accurate classification

qbdza30fc0a49919
diff --git a/.vitest-out.tmp b/.vitest-out.tmp
new file mode 100644
index 000000000..3c710fc26
--- /dev/null
+++ b/.vitest-out.tmp
@@ -0,0 +1,43 @@
+
+ RUN v4.1.7 /workspaces/three.ws
+
+······································x··stdout | tests/api/purchase-as-agent.test.js > POST /api/marketplace/purchase-as-agent > returns 429 when per-agent rate limit exceeded
+{"ts":"2026-06-29T22:44:26.915Z","event":"purchase_as_agent.rate_limited","buyer_agent_id":"00000000-0000-0000-0000-000000000001","seller_agent_id":"00000000-0000-0000-0000-000000000002","skill":"pro"}
+
+stdout | tests/api/purchase-as-agent.test.js > POST /api/marketplace/purchase-as-agent > returns 200 already_owned when confirmed purchase exists
+{"ts":"2026-06-29T22:44:26.952Z","event":"purchase_as_agent.already_owned","buyer_agent_id":"00000000-0000-0000-0000-000000000001","seller_agent_id":"00000000-0000-0000-0000-000000000002","skill":"pro"}
+
+······stdout | tests/api/purchase-as-agent.test.js > POST /api/marketplace/purchase-as-agent > returns 402 when daily spend cap would be exceeded
+{"ts":"2026-06-29T22:44:26.969Z","event":"purchase_as_agent.spend_cap_exceeded","buyer_agent_id":"00000000-0000-0000-0000-000000000001","skill":"pro","spent":"500000","amount":1000000,"limit_atomics":"500000"}
+
+stdout | tests/api/purchase-as-agent.test.js > POST /api/marketplace/purchase-as-agent > returns 200 confirmed on happy path
+{"ts":"2026-06-29T22:44:26.985Z","event":"purchase_as_agent.pending","purchase_id":"ppp","buyer_agent_id":"00000000-0000-0000-0000-000000000001","seller_agent_id":"00000000-0000-0000-0000-000000000002","skill":"pro","amount":1000000,"is_self_dealing":false}
+
+stdout | tests/api/purchase-as-agent.test.js > POST /api/marketplace/purchase-as-agent > returns 200 confirmed on happy path
+{"ts":"2026-06-29T22:44:26.999Z","event":"purchase_as_agent.tx_confirmed","purchase_id":"ppp","tx_signature":"txSig"}
+
+stdout | tests/api/purchase-as-agent.test.js > POST /api/marketplace/purchase-as-agent > returns 200 confirmed on happy path
+{"ts":"2026-06-29T22:44:27.008Z","event":"purchase_as_agent.confirmed","purchase_id":"ppp","buyer_agent_id":"00000000-0000-0000-0000-000000000001","seller_agent_id":"00000000-0000-0000-0000-000000000002","skill":"pro","amount":1000000,"tx_signature":"sig123","is_self_dealing":false}
+
+stdout | tests/api/purchase-as-agent.test.js > POST /api/marketplace/purchase-as-agent > flags self-dealing but does not block it
+{"ts":"2026-06-29T22:44:27.023Z","event":"purchase_as_agent.self_dealing","buyer_agent_id":"00000000-0000-0000-0000-000000000001","seller_agent_id":"00000000-0000-0000-0000-000000000002","skill":"pro","user_id":"aaaa0000-0000-0000-0000-000000000001"}
+
+stdout | tests/api/purchase-as-agent.test.js > POST /api/marketplace/purchase-as-agent > flags self-dealing but does not block it
+{"ts":"2026-06-29T22:44:27.024Z","event":"purchase_as_agent.pending","purchase_id":"ppp","buyer_agent_id":"00000000-0000-0000-0000-000000000001","seller_agent_id":"00000000-0000-0000-0000-000000000002","skill":"pro","amount":1000000,"is_self_dealing":true}
+
+stdout | tests/api/purchase-as-agent.test.js > POST /api/marketplace/purchase-as-agent > flags self-dealing but does not block it
+{"ts":"2026-06-29T22:44:27.030Z","event":"purchase_as_agent.tx_confirmed","purchase_id":"ppp","tx_signature":"txSig"}
+
+stdout | tests/api/purchase-as-agent.test.js > POST /api/marketplace/purchase-as-agent > flags self-dealing but does not block it
+{"ts":"2026-06-29T22:44:27.035Z","event":"purchase_as_agent.confirmed","purchase_id":"ppp","buyer_agent_id":"00000000-0000-0000-0000-000000000001","seller_agent_id":"00000000-0000-0000-0000-000000000002","skill":"pro","amount":1000000,"tx_signature":"sig123","is_self_dealing":true}
+
+stdout | tests/api/purchase-as-agent.test.js > POST /api/marketplace/purchase-as-agent > returns 502 when the on-chain transaction fails
+{"ts":"2026-06-29T22:44:27.045Z","event":"purchase_as_agent.pending","purchase_id":"ppp","buyer_agent_id":"00000000-0000-0000-0000-000000000001","seller_agent_id":"00000000-0000-0000-0000-000000000002","skill":"pro","amount":1000000,"is_self_dealing":false}
+
+stdout | tests/api/purchase-as-agent.test.js > POST /api/marketplace/purchase-as-agent > returns 502 when the on-chain transaction fails
+{"ts":"2026-06-29T22:44:27.045Z","event":"purchase_as_agent.tx_failed","purchase_id":"ppp","error":"insufficient funds","buyer_agent_id":"00000000-0000-0000-0000-000000000001"}
+
+······x········xx······x········stderr | tests/api/providers-nvidia.test.js > nvidia provider — 202-then-poll loop > reports failed when a finished poll carries no GLB artifact
+[nvidia] poll 200 but no GLB artifact — json keys=["artifacts"] artifacts=[]
+
+······································
\ No newline at end of file
diff --git a/api/_lib/db.js b/api/_lib/db.js
index 99a216b3f..ff656624e 100644
--- a/api/_lib/db.js
+++ b/api/_lib/db.js
@@ -185,6 +185,23 @@ export function isDbUnavailableError(err) {
// which survives minification. Fall back to constructor.name for dev builds.
const name = String(err.name || err.constructor?.name || '');
const msg = String(err.message ?? '');
+ // Neon wraps the underlying transport failure: a NeonDbError carries the real
+ // cause on `.sourceError` (and sometimes `.cause`), e.g. a `fetch failed`
+ // TypeError. Fold those in so a connection-level failure is recognized even
+ // when Neon's own message is the generic "Error connecting to database: …".
+ const deepMsg = `${msg} ${err.sourceError?.message ?? ''} ${err.cause?.message ?? ''}`;
+ // Connection-level transport failures shared across error classes — the
+ // request never reached Postgres (DNS/TLS/socket/cold-compute wake) or the
+ // connection was dropped mid-flight. All are transient "temporarily
+ // unavailable, retry" conditions (→ 503 + Retry-After), never deterministic
+ // SQL bugs. Mirrors isTransientConnError() in db-retry.js so the graceful-503
+ // classifier and the write-retry guard agree on what counts as a blip. NOTE:
+ // deliberately excludes statement-level errors (syntax, constraint, undefined
+ // table/column, 22021 NUL) — those are real faults that must keep 500ing.
+ const isConnLevel =
+ /Error connecting to database|fetch failed|ECONNRESET|ECONNREFUSED|ETIMEDOUT|EAI_AGAIN|socket hang up|Connection terminated|terminating connection|too many connections|remaining connection slots|Client has encountered a connection error/i.test(
+ deepMsg,
+ );
// Construction/configuration failures: a missing, empty, or malformed
// DATABASE_URL makes the env accessor (`Missing required env var:
// DATABASE_URL`) or neon() (`No database connection string was provided…`,
@@ -203,9 +220,8 @@ export function isDbUnavailableError(err) {
}
if (name === 'NeonDbError' || name === 'DatabaseError') {
return (
+ isConnLevel ||
msg.includes('password authentication failed') ||
- msg.includes('connection refused') ||
- msg.includes('ECONNREFUSED') ||
msg.includes('SSL connection') ||
// Neon phrases a suspended compute as "The endpoint has been disabled.
// Enable it using Neon API and retry." Keep the older 'is disabled'
@@ -215,10 +231,12 @@ export function isDbUnavailableError(err) {
msg.includes('Control plane request failed')
);
}
- // Network-level fetch failure wrapping a Neon request (rare but happens when
- // the Neon HTTP gateway is unreachable).
+ // Network-level fetch failure wrapping a Neon request (happens when the Neon
+ // HTTP gateway is unreachable, or a cold compute is waking from scale-to-zero —
+ // the driver surfaces a bare TypeError: fetch failed before it ever becomes a
+ // NeonDbError).
if (name === 'FetchError' || name === 'TypeError') {
- return msg.includes('ECONNREFUSED') || msg.includes('fetch failed');
+ return isConnLevel;
}
// Upstash Redis auth failure — WRONGPASS means the token is wrong or rotated.
// Same "infra misconfigured" class as a DB auth failure: a single bad env var
diff --git a/tests/db-unconfigured.test.js b/tests/db-unconfigured.test.js
index 0568eb504..30a7db74f 100644
--- a/tests/db-unconfigured.test.js
+++ b/tests/db-unconfigured.test.js
@@ -84,4 +84,45 @@ describe('db.js with an unconfigured DATABASE_URL', () => {
it('still surfaces sqlValues build-time validation as a real throw (not masked)', () => {
expect(() => sqlValues([])).toThrow();
});
+
+ // A live Neon outage / cold-compute wake does NOT surface as a missing env var
+ // — the connection string is present but the HTTP transport fails. Neon's
+ // driver wraps that as `NeonDbError: Error connecting to database: fetch
+ // failed`. This used to fall through every branch (the NeonDbError branch never
+ // checked 'fetch failed'; the TypeError branch never ran for a NeonDbError) and
+ // 500-stormed every DB endpoint at once instead of degrading to a single 503.
+ it('classifies a Neon connection-level transport failure as db-unavailable (→ 503, not 500)', () => {
+ const fetchFailed = Object.assign(new Error('Error connecting to database: fetch failed'), {
+ name: 'NeonDbError',
+ });
+ expect(isDbUnavailableError(fetchFailed)).toBe(true);
+
+ // Same failure when the cause is carried on .sourceError rather than the message.
+ const wrapped = Object.assign(new Error('Error connecting to database'), {
+ name: 'NeonDbError',
+ sourceError: new TypeError('fetch failed'),
+ });
+ expect(isDbUnavailableError(wrapped)).toBe(true);
+
+ for (const m of [
+ 'Connection terminated unexpectedly',
+ 'terminating connection due to administrator command',
+ 'sorry, too many clients already / remaining connection slots are reserved',
+ 'read ECONNRESET',
+ 'connect ETIMEDOUT',
+ ]) {
+ expect(isDbUnavailableError(Object.assign(new Error(m), { name: 'NeonDbError' }))).toBe(true);
+ }
+ });
+
+ it('still 500s on a real SQL fault (syntax / undefined column) — not masked as 503', () => {
+ const syntax = Object.assign(new Error('syntax error at or near "$3"'), { name: 'NeonDbError' });
+ expect(isDbUnavailableError(syntax)).toBe(false);
+ const undefinedCol = Object.assign(new Error('column "nope" does not exist'), { name: 'NeonDbError' });
+ expect(isDbUnavailableError(undefinedCol)).toBe(false);
+ });
+
+ it('classifies a bare `fetch failed` TypeError (pre-wrap transport error)', () => {
+ expect(isDbUnavailableError(new TypeError('fetch failed'))).toBe(true);
+ });
});