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.

Stop a deliberate 503 from collapsing into a generic internal_error

nirholasf5acd974d62b
diff --git a/api/_lib/gateway.js b/api/_lib/gateway.js
index 0438d0b9b..03f362e8a 100644
--- a/api/_lib/gateway.js
+++ b/api/_lib/gateway.js
@@ -49,7 +49,10 @@ import { recordEvent } from './usage.js';
* @param {string} message
*/
export function fail(status, code, message) {
- throw Object.assign(new Error(message), { status, code });
+ // `expose` marks this as an author-written contract error, so wrap() may echo
+ // the code and message even on a 5xx. Errors that merely bubble up (a Postgres
+ // SQLSTATE, an ECONNREFUSED) carry no such marker and stay redacted.
+ throw Object.assign(new Error(message), { status, code, expose: true });
}
/**
diff --git a/api/_lib/http.js b/api/_lib/http.js
index 5efba94ad..9482697cd 100644
--- a/api/_lib/http.js
+++ b/api/_lib/http.js
@@ -558,6 +558,20 @@ export function wrap(handler) {
error_description: `this endpoint is not configured on this deployment — quote ref ${ref} to support`,
ref,
}, { 'cache-control': 'no-store' });
+ } else if (!dbDegraded && err.expose && err.code) {
+ // An author-written contract error: the endpoint deliberately threw
+ // this status, code, and message (gateway `fail()`, or an explicit
+ // `Object.assign(new Error(...), { status, code, expose: true })`).
+ // These are documented parts of the endpoint's contract and are what
+ // a caller needs to react correctly, so a `503 upstream_unavailable`
+ // must not collapse into a generic internal_error. The `expose`
+ // marker is the whole gate: an error that merely bubbled up never
+ // carries it, so the redaction below still covers every leak case.
+ json(res, status, {
+ error: err.code,
+ error_description: err.message || `error, quote ref ${ref} to support`,
+ ref,
+ }, { 'cache-control': 'no-store' });
} else {
// `err.code` is NOT safe to echo here, for the same reason the
// message above is redacted: on a 5xx it carries internal
diff --git a/api/forge-nim.js b/api/forge-nim.js
index c03d2bbac..1e619db1d 100644
--- a/api/forge-nim.js
+++ b/api/forge-nim.js
@@ -118,7 +118,9 @@ function resolveBaseUrl(requested) {
new Error(
'No self-hosted TRELLIS NIM is configured. Set MODEL_TRELLIS_URL on the deployment, or pass a baseUrl pointing at your own NIM (nvcr.io/nim/microsoft/trellis).',
),
- { status: 503, code: 'nim_unconfigured' },
+ // expose: this is a documented contract error, not a leaked runtime code,
+ // so wrap() may hand the caller the real reason instead of internal_error.
+ { status: 503, code: 'nim_unconfigured', expose: true },
);
}
return configured.replace(/\/$/, '');
diff --git a/tests/api/http-server-error-page.test.js b/tests/api/http-server-error-page.test.js
index 35c8fc735..b547bfb6a 100644
--- a/tests/api/http-server-error-page.test.js
+++ b/tests/api/http-server-error-page.test.js
@@ -86,3 +86,49 @@ describe('wrap() 5xx navigation routing', () => {
expect(res.headers['location']).toBeUndefined();
});
});
+
+// A 5xx has two very different sources, and they need opposite handling: an error
+// the endpoint author wrote (`503 upstream_unavailable`) is contract a caller must
+// be able to react to, while an error that merely bubbled up carries internal
+// detail (SQLSTATE, ECONNREFUSED, a keyed RPC URL) and must stay redacted. The
+// `expose` marker is the only thing that separates them.
+describe('wrap() 5xx code exposure', () => {
+ it('hands back a deliberate contract code and message', async () => {
+ const handler = wrap(async () => {
+ throw Object.assign(new Error('pump.fun data source is temporarily unreachable'), {
+ status: 503,
+ code: 'upstream_unavailable',
+ expose: true,
+ });
+ });
+ const req = mockReq({ headers: { accept: 'application/json' } });
+ const res = mockRes();
+ await handler(req, res);
+
+ expect(res.statusCode).toBe(503);
+ const body = JSON.parse(res.body);
+ expect(body.error).toBe('upstream_unavailable');
+ expect(body.error_description).toContain('temporarily unreachable');
+ expect(body.ref).toMatch(/^[0-9a-f]{16}$/);
+ });
+
+ it('still redacts a 5xx code that was never marked as contract', async () => {
+ const handler = wrap(async () => {
+ // Shaped like a Postgres failure: the code names the storage engine's fault
+ // and the message carries the credentialed host.
+ throw Object.assign(new Error('relation "users" does not exist at db://user:pw@host'), {
+ status: 500,
+ code: '42P01',
+ });
+ });
+ const req = mockReq({ headers: { accept: 'application/json' } });
+ const res = mockRes();
+ await handler(req, res);
+
+ expect(res.statusCode).toBe(500);
+ const body = JSON.parse(res.body);
+ expect(body.error).toBe('internal_error');
+ expect(res.body).not.toContain('42P01');
+ expect(res.body).not.toContain('user:pw@host');
+ });
+});