Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

feat(proxy): mark unhandled-error 500s with x-claudish-unhandled-error

Jack Rudenko25763f1030f5
diff --git a/packages/cli/src/handlers/shared/hop-decision.ts b/packages/cli/src/handlers/shared/hop-decision.ts
index 0e51abb0..5146b91e 100644
--- a/packages/cli/src/handlers/shared/hop-decision.ts
+++ b/packages/cli/src/handlers/shared/hop-decision.ts
@@ -27,6 +27,17 @@ import {
} from "./recovery-marker.js";
import { isSubscriptionProvider } from "./remote-provider-types.js";
+/**
+ * The marker the proxy puts on a 500 that means a handler THREW instead of answering:
+ * the `/v1/messages` catch when the resolved handler threw, and `app.onError` for a
+ * rejection that escaped it. Inside a routing chain `FallbackHandler` catches a throw
+ * and advances; a probe pins ONE hop, so it gets the 500 instead. Read as a response, a
+ * plain 500 is non-retryable, so the probe would say "the chain stops here" for a hop
+ * the runtime advances past. The marker lets the probe judge it as `thrown`. It changes
+ * nothing for Claude Code: the status, body and envelope are the same.
+ */
+export const UNHANDLED_ERROR_HEADER = "x-claudish-unhandled-error";
+
/** Why a chain advanced or stopped at a failed hop. */
export type HopFailureReason =
/** claudish's own recovery hold (`x-claudish-recovery`): stop, response verbatim. */
diff --git a/packages/cli/src/proxy-server.ts b/packages/cli/src/proxy-server.ts
index ff0f3f0a..69fedea5 100644
--- a/packages/cli/src/proxy-server.ts
+++ b/packages/cli/src/proxy-server.ts
@@ -30,6 +30,7 @@ import {
createSigningSecrets,
releaseWhenBodyEnds,
} from "./handlers/shared/call-context.js";
+import { UNHANDLED_ERROR_HEADER } from "./handlers/shared/hop-decision.js";
import type { ModelHandler } from "./handlers/types.js";
import type { HookEngine } from "./hooks/engine.js";
import { loadHooksOnce } from "./hooks/index.js";
@@ -886,9 +887,12 @@ export async function createProxyServer(
app.onError((err, c) => {
logStderr(`[Proxy] Unhandled error on ${c.req.method} ${c.req.path}: ${err?.message ?? err}`);
log(`[Proxy] Unhandled error stack: ${err?.stack ?? "(no stack)"}`);
+ // Marked so a probe of one pinned hop can tell a throw from an upstream 500
+ // (`UNHANDLED_ERROR_HEADER`); the status and envelope are unchanged.
return c.json(
wrapAnthropicError(500, redactForClient(`Proxy error: ${err?.message ?? String(err)}`)),
- 500
+ 500,
+ { [UNHANDLED_ERROR_HEADER]: "1" }
);
});
@@ -1069,6 +1073,9 @@ export async function createProxyServer(
});
app.post("/v1/messages", async (c) => {
+ // Set when the resolved handler itself threw (not the body parse, not routing):
+ // that 500 is marked `UNHANDLED_ERROR_HEADER`, like `app.onError`'s.
+ let handlerThrew = false;
try {
const body = await c.req.json();
// Request-metadata trace (debug log only — the [RequestMeta] prefix is
@@ -1117,7 +1124,12 @@ export async function createProxyServer(
// the promise back BEFORE it settles, so a rejection escapes this
// try/catch entirely and lands in Hono's error handler instead. That is
// how connect failures bypassed every message we build here.
- return await handler.handle(c, body);
+ try {
+ return await handler.handle(c, body);
+ } catch (handlerError) {
+ handlerThrew = true;
+ throw handlerError;
+ }
} catch (e) {
log(`[Proxy] Error: ${e}`);
// Routing failures are terminal — surface as a non-retryable 400 so the
@@ -1129,7 +1141,14 @@ export async function createProxyServer(
400
);
}
- return c.json(wrapAnthropicError(500, redactForClient(String(e))), 500);
+ // A handler that threw is a hop that threw: inside a routing chain
+ // FallbackHandler advances past it, so a probe of one pinned hop must be
+ // able to tell it from an upstream 500 (`UNHANDLED_ERROR_HEADER`).
+ return c.json(
+ wrapAnthropicError(500, redactForClient(String(e))),
+ 500,
+ handlerThrew ? { [UNHANDLED_ERROR_HEADER]: "1" } : undefined
+ );
}
});