Skip to content

The agent that grows with you

fix(shared): a failed gateway dial says which failure it hit

kshitijk4poorefe042abe921
diff --git a/apps/shared/src/json-rpc-gateway-connect.test.ts b/apps/shared/src/json-rpc-gateway-connect.test.ts
new file mode 100644
index 00000000000..25e10b719fd
--- /dev/null
+++ b/apps/shared/src/json-rpc-gateway-connect.test.ts
@@ -0,0 +1,75 @@
+import { describe, expect, it } from 'vitest'
+
+import { JsonRpcGatewayClient } from './json-rpc-gateway'
+
+/** EventTarget-based WebSocket stand-in that never opens, so each failure leg can be driven by hand. */
+class StuckSocket extends EventTarget {
+ static OPEN = 1
+ static last: StuckSocket | null = null
+
+ readyState = 0
+
+ constructor() {
+ super()
+ StuckSocket.last = this
+ }
+
+ send(): void {}
+
+ close(): void {
+ this.readyState = 3
+ }
+}
+
+const connectErrorMessage = 'Could not connect to Hermes gateway'
+
+const rejection = (pending: Promise<void>): Promise<Error> =>
+ pending.then(
+ () => new Error('connect resolved'),
+ (error: Error) => error
+ )
+
+const dial = (connectTimeoutMs = 1000) => {
+ const client = new JsonRpcGatewayClient({
+ socketFactory: () => new StuckSocket() as unknown as WebSocket,
+ heartbeatIntervalMs: 0,
+ heartbeatDeadlineMs: 0,
+ connectTimeoutMs,
+ connectErrorMessage
+ })
+
+ const pending = client.connect('ws://gateway.test/api/ws')
+ pending.catch(() => {})
+
+ return { client, pending, socket: StuckSocket.last as StuckSocket }
+}
+
+// Regression for #41566: the overlay showed the same sentence for an auth rejection, a TLS failure and a
+// silent host, so users verified the gateway with curl and still could not tell what the app hit.
+describe('JsonRpcGatewayClient.connect failure classes', () => {
+ it('a handshake close carries its code and reason, distinct from a timeout', async () => {
+ const closed = dial()
+ closed.socket.dispatchEvent(new CloseEvent('close', { code: 4403, reason: 'token_mismatch' }))
+ const closeError = await rejection(closed.pending)
+
+ const timedOut = dial(20)
+ const timeoutError = await rejection(timedOut.pending)
+
+ expect(closeError.message).toContain(connectErrorMessage)
+ expect(closeError.message).toContain('4403')
+ expect(closeError.message).toContain('token_mismatch')
+ expect(timeoutError.message).toContain(connectErrorMessage)
+ expect(timeoutError.message).toContain('20 ms')
+ expect(timeoutError.message).not.toBe(closeError.message)
+ expect(timedOut.client.connectionState).toBe('error')
+ })
+
+ it('an error before open still names the base message so existing "sidecar down" matchers keep working', async () => {
+ const { pending, socket } = dial()
+ socket.dispatchEvent(new Event('error'))
+ const error = await rejection(pending)
+
+ expect(error.message.startsWith(connectErrorMessage)).toBe(true)
+ expect(error.message).toContain('error before open')
+ })
+})
diff --git a/apps/shared/src/json-rpc-gateway.ts b/apps/shared/src/json-rpc-gateway.ts
index 365e6d0feb9..9637ea0b598 100644
--- a/apps/shared/src/json-rpc-gateway.ts
+++ b/apps/shared/src/json-rpc-gateway.ts
@@ -252,7 +252,10 @@ export class JsonRpcGatewayClient {
void this.fetchReplay()
}
- const onError = () => {
+ // Every rejection below names its failure class. The boot overlay renders this message verbatim, and
+ // a bare connectErrorMessage collapses "server refused the token", "TLS/DNS/refused before open" and
+ // "nothing answered" into one sentence nobody can act on (#41566).
+ const onError = (event: Event) => {
if (settled || this.socket !== socket) {
return
}
@@ -260,7 +263,10 @@ export class JsonRpcGatewayClient {
settled = true
cleanup()
this.setState('error')
- reject(new Error(this.options.connectErrorMessage))
+ const detail = (event as { message?: unknown }).message
+ reject(
+ this.connectFailure(`WebSocket error before open${typeof detail === 'string' && detail ? `: ${detail}` : ''}`)
+ )
}
// A server that closes during the handshake (auth gate, 4401/4403)
@@ -270,7 +276,7 @@ export class JsonRpcGatewayClient {
// and moved the generation to 'closed'; the branch below only runs
// when `onSocketClose` intercepted that transition and left the
// half-open socket bound.
- const onClose = () => {
+ const onClose = (event: Event) => {
if (settled) {
return
}
@@ -283,7 +289,12 @@ export class JsonRpcGatewayClient {
this.setState('error')
}
- reject(new Error(this.options.connectErrorMessage))
+ const { code, reason } = event as { code?: unknown; reason?: unknown }
+ reject(
+ this.connectFailure(
+ `WebSocket closed during handshake: code ${typeof code === 'number' ? code : 'unknown'}${typeof reason === 'string' && reason ? ` ${reason}` : ''}`
+ )
+ )
}
socket.addEventListener('open', onOpen, { once: true })
@@ -312,12 +323,16 @@ export class JsonRpcGatewayClient {
this.setState('error')
}
- reject(new Error(this.options.connectErrorMessage))
+ reject(this.connectFailure(`no WebSocket open within ${this.options.connectTimeoutMs} ms`))
}, this.options.connectTimeoutMs)
}
})
}
+ private connectFailure(detail: string): Error {
+ return new Error(`${this.options.connectErrorMessage} (${detail})`)
+ }
+
close(): void {
this.invalidate()
}