-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: improve error handling when starting server fails (#261)
* feature: improve error handling when starting server fails * update code * feature: add getFirstEvent function * lint * lint * fix * fix * test * test * test * lint * lint
- Loading branch information
Showing
3 changed files
with
55 additions
and
16 deletions.
There are no files selected for viewing
13 changes: 10 additions & 3 deletions
13
packages/preview-process/src/parts/WaitForServerToBeReady/WaitForServerToBeReady.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
import type { WebViewServer } from '../WebViewServerTypes/WebViewServerTypes.ts' | ||
import * as GetFirstEvent from '../GetFirstEvent/GetFirstEvent.ts' | ||
|
||
export const waitForServerToBeReady = async (server: WebViewServer, port: string): Promise<void> => { | ||
const { resolve, promise } = Promise.withResolvers<void>() | ||
server.listen(port, resolve) | ||
await promise | ||
const responsePromise = GetFirstEvent.getFirstEvent(server as any, { | ||
error: 1, | ||
listening: 2, | ||
}) | ||
server.listen(port, () => {}) | ||
const { type, event } = await responsePromise | ||
if (type === 1) { | ||
throw new Error(`Server error: ${event}`) | ||
} | ||
} |
53 changes: 43 additions & 10 deletions
53
packages/preview-process/test/WaitForServerToBeReady.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,49 @@ | ||
import { expect, jest, test } from '@jest/globals' | ||
import { expect, test } from '@jest/globals' | ||
import { EventEmitter } from 'node:events' | ||
import type { WebViewServer } from '../src/parts/WebViewServerTypes/WebViewServerTypes.ts' | ||
import * as WaitForServerToBeReady from '../src/parts/WaitForServerToBeReady/WaitForServerToBeReady.ts' | ||
|
||
test('waitForServerToBeReady', async () => { | ||
const mockServer = { | ||
listen: jest.fn((port, callback) => { | ||
// @ts-ignore | ||
class MockServer extends EventEmitter implements WebViewServer { | ||
handler = undefined | ||
listening = false | ||
|
||
setHandler(handler: any): void { | ||
this.handler = handler | ||
} | ||
|
||
listen(port: string, callback: () => void): void { | ||
process.nextTick(() => { | ||
this.listening = true | ||
this.emit('listening') | ||
callback() | ||
}), | ||
}) | ||
} | ||
isListening(): boolean { | ||
return this.listening | ||
} | ||
} | ||
|
||
test('waitForServerToBeReady - success', async () => { | ||
const server = new MockServer() | ||
const port = '3000' | ||
await WaitForServerToBeReady.waitForServerToBeReady(server, port) | ||
expect(server.listening).toBe(true) | ||
}) | ||
|
||
test('waitForServerToBeReady - handles EADDRINUSE error', async () => { | ||
const server = new MockServer() | ||
const port = '3000' | ||
process.nextTick(() => { | ||
server.emit('error', new Error('EADDRINUSE')) | ||
}) | ||
await expect(WaitForServerToBeReady.waitForServerToBeReady(server, port)).rejects.toThrow('EADDRINUSE') | ||
}) | ||
|
||
test('waitForServerToBeReady - handles other errors', async () => { | ||
const server = new MockServer() | ||
const port = '3000' | ||
// @ts-ignore | ||
await WaitForServerToBeReady.waitForServerToBeReady(mockServer, port) | ||
expect(mockServer.listen).toHaveBeenCalledTimes(1) | ||
expect(mockServer.listen).toHaveBeenCalledWith(port, expect.any(Function)) | ||
process.nextTick(() => { | ||
server.emit('error', new Error('Some other error')) | ||
}) | ||
await expect(WaitForServerToBeReady.waitForServerToBeReady(server, port)).rejects.toThrow('Some other error') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters