diff --git a/packages/core/src/ide/ide-client.ts b/packages/core/src/ide/ide-client.ts index e9d25f1c013..25793f9956a 100644 --- a/packages/core/src/ide/ide-client.ts +++ b/packages/core/src/ide/ide-client.ts @@ -495,23 +495,13 @@ export class IdeClient { } } - private registerClientHandlers() { + // Must be invoked before client.connect so that errors surfaced during + // connection setup or by a long-lived SSE stream are routed through + // onerror instead of becoming unhandled promise rejections. + private registerTransportHandlers() { if (!this.client) { return; } - - this.client.setNotificationHandler( - IdeContextNotificationSchema, - (notification) => { - ideContextStore.set(notification.params); - const isTrusted = notification.params.workspaceState?.isTrusted; - if (isTrusted !== undefined) { - for (const listener of this.trustChangeListeners) { - listener(isTrusted); - } - } - }, - ); this.client.onerror = (_error) => { const errorMessage = _error instanceof Error ? _error.message : `_error`; this.setState( @@ -527,6 +517,25 @@ export class IdeClient { true, ); }; + } + + private registerClientHandlers() { + if (!this.client) { + return; + } + + this.client.setNotificationHandler( + IdeContextNotificationSchema, + (notification) => { + ideContextStore.set(notification.params); + const isTrusted = notification.params.workspaceState?.isTrusted; + if (isTrusted !== undefined) { + for (const listener of this.trustChangeListeners) { + listener(isTrusted); + } + } + }, + ); this.client.setNotificationHandler( IdeDiffAcceptedNotificationSchema, (notification) => { @@ -597,6 +606,7 @@ export class IdeClient { headers: authToken ? { Authorization: `Bearer ${authToken}` } : {}, }, }); + this.registerTransportHandlers(); await this.client.connect(transport); this.registerClientHandlers(); await this.discoverTools(); @@ -630,6 +640,7 @@ export class IdeClient { command, args, }); + this.registerTransportHandlers(); await this.client.connect(transport); this.registerClientHandlers(); await this.discoverTools();