Skip to content

Commit 9736e31

Browse files
rekmarksclaude
andcommitted
refactor(kernel-browser-runtime): Simplify error handling in internal-connections
Remove overly complex error handling logic that differentiated between handler initialization errors and message handling errors. Replace with simple one-liner handler resolution and single catch block. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3a40d6a commit 9736e31

File tree

2 files changed

+19
-57
lines changed

2 files changed

+19
-57
lines changed

packages/kernel-browser-runtime/src/internal-comms/internal-connections.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,11 @@ describe('internal-connections', () => {
526526

527527
it('should queue messages until handlerPromise resolves', async () => {
528528
let resolveHandler: (handler: typeof mockHandleMessage) => void;
529-
const handlerPromise = new Promise<typeof mockHandleMessage>((resolve) => {
530-
resolveHandler = resolve;
531-
});
529+
const handlerPromise = new Promise<typeof mockHandleMessage>(
530+
(resolve) => {
531+
resolveHandler = resolve;
532+
},
533+
);
532534

533535
receiveInternalConnections({
534536
handlerPromise,
@@ -592,6 +594,8 @@ describe('internal-connections', () => {
592594
it('should handle handlerPromise rejection', async () => {
593595
const handlerError = new Error('Handler initialization failed');
594596
const handlerPromise = Promise.reject(handlerError);
597+
// Prevent unhandled rejection warning - error is handled by receiveInternalConnections
598+
handlerPromise.catch(() => undefined);
595599

596600
receiveInternalConnections({
597601
handlerPromise,
@@ -629,7 +633,7 @@ describe('internal-connections', () => {
629633
await delay();
630634

631635
expect(logger.error).toHaveBeenCalledWith(
632-
'Error initializing message handler for internal process "internal-process-channel":',
636+
'Error handling message from internal process "internal-process-channel":',
633637
handlerError,
634638
);
635639
});

packages/kernel-browser-runtime/src/internal-comms/internal-connections.ts

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -123,39 +123,18 @@ type ReceiveConnectionsOptions = Omit<Options, 'label'> &
123123
* @param options.handler - The function to handle internal messages. Mutually exclusive
124124
* with `handlerPromise`.
125125
* @param options.handlerPromise - A promise that resolves to the handler function.
126-
* Incoming messages await handler initialization once, then the resolved handler is
127-
* cached and used directly for all subsequent messages. Mutually exclusive with `handler`.
126+
* Mutually exclusive with `handler`.
128127
* @param options.logger - The logger instance.
129128
* @param options.controlChannelName - The name of the control channel. Must match
130129
* the name used by {@link connectToKernel} on the other end.
131130
*/
132131
export const receiveInternalConnections = ({
133-
handler: directHandler,
132+
handler,
134133
handlerPromise,
135134
logger,
136135
controlChannelName = COMMS_CONTROL_CHANNEL_NAME,
137136
}: ReceiveConnectionsOptions): void => {
138-
// Support both direct handler and promise-based handler
139-
let handler: HandleInternalMessage | null = null;
140-
let handlerResolution: Promise<HandleInternalMessage>;
141-
142-
if (directHandler !== undefined) {
143-
// Direct handler - use immediately
144-
handler = directHandler;
145-
handlerResolution = Promise.resolve(directHandler);
146-
} else {
147-
// Promise-based handler - cache once resolved
148-
handlerResolution = handlerPromise.then(
149-
(resolvedHandler) => {
150-
handler = resolvedHandler;
151-
return resolvedHandler;
152-
},
153-
(error) => {
154-
// Re-throw to propagate initialization errors to message handlers
155-
throw error;
156-
},
157-
);
158-
}
137+
const handlerResolution = handler ? Promise.resolve(handler) : handlerPromise;
159138

160139
const seenChannels = new Set<string>();
161140
new BroadcastChannel(controlChannelName).onmessage = (event) => {
@@ -183,39 +162,18 @@ export const receiveInternalConnections = ({
183162
`Received message from internal process "${channelName}": ${JSON.stringify(message)}`,
184163
);
185164

186-
try {
187-
// Use cached handler if available, otherwise await once
188-
const messageHandler = handler ?? (await handlerResolution);
189-
const reply = await messageHandler(message);
190-
if (reply !== undefined) {
191-
await kernelRpcStream.write(reply);
192-
}
193-
} catch (error) {
194-
// Check if this is a handler initialization error
195-
if (handler === null) {
196-
logger.error(
197-
`Error initializing message handler for internal process "${channelName}":`,
198-
error,
199-
);
200-
} else {
201-
logger.error(
202-
`Error handling message from internal process "${channelName}":`,
203-
error,
204-
);
205-
}
206-
throw error;
165+
const messageHandler = await handlerResolution;
166+
const reply = await messageHandler(message);
167+
if (reply !== undefined) {
168+
await kernelRpcStream.write(reply);
207169
}
208170
});
209171
})
210172
.catch((error) => {
211-
// This catch handles connection errors and re-thrown handler errors
212-
if (handler !== null) {
213-
logger.error(
214-
`Error handling message from internal process "${channelName}":`,
215-
error,
216-
);
217-
}
218-
// Initialization errors are already logged in the try-catch above
173+
logger.error(
174+
`Error handling message from internal process "${channelName}":`,
175+
error,
176+
);
219177
})
220178
.finally(() => {
221179
logger.debug(`Closed connection to internal process "${channelName}"`);

0 commit comments

Comments
 (0)