diff --git a/packages/preview-process/src/parts/WebViewServerState/WebViewServerState.ts b/packages/preview-process/src/parts/WebViewServerState/WebViewServerState.ts index 32ac2dca..fb927cc8 100644 --- a/packages/preview-process/src/parts/WebViewServerState/WebViewServerState.ts +++ b/packages/preview-process/src/parts/WebViewServerState/WebViewServerState.ts @@ -1,13 +1,17 @@ import type { WebViewServer } from '../WebViewServerTypes/WebViewServerTypes.ts' -const servers = Object.create(null) +const servers: Record = Object.create(null) export const set = (id: number, server: WebViewServer): void => { servers[id] = server } export const get = (id: number): WebViewServer => { - return servers[id] + const server = servers[id] + if (!server) { + throw new Error(`Server with id ${id} not found`) + } + return server } export const has = (id: number): boolean => { diff --git a/packages/test-integration/test/PreviewProcessStartWithoutCreate.test.ts b/packages/test-integration/test/PreviewProcessStartWithoutCreate.test.ts index 08474467..5ef87f6c 100644 --- a/packages/test-integration/test/PreviewProcessStartWithoutCreate.test.ts +++ b/packages/test-integration/test/PreviewProcessStartWithoutCreate.test.ts @@ -8,7 +8,7 @@ test('preview process - handles port already in use', async () => { const port = await getPort() // TODO improve error message await expect(previewProcess1.invoke('WebViewServer.start', id, port)).rejects.toThrow( - "Failed to start webview server: TypeError: Cannot read properties of undefined (reading 'server')", + 'Failed to start webview server: Server with id 1 not found', ) previewProcess1[Symbol.dispose]() })