Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/sdk-js/src/v2/live/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,43 @@ describe('LiveV2Session connectSession', () => {
)
expect(session.sessionId).toBe('created-session-id')
})

it('surfaces init failure without an unhandled rejection', async () => {
const initError = new Error('init failed')
mockHttpPost.mockRejectedValue(initError)

const unhandledRejections: unknown[] = []
const onUnhandledRejection = (reason: unknown) => {
unhandledRejections.push(reason)
}
process.on('unhandledRejection', onUnhandledRejection)

try {
const errorSpy = vi.fn()
const endedSpy = vi.fn()

const session = new LiveV2Session({
options: { sample_rate: 16000 },
httpClient,
webSocketClient,
})

session.on('error', errorSpy)
session.on('ended', endedSpy)

await expect(session.getSessionId()).rejects.toThrow('init failed')
await tick()

expect(errorSpy).toHaveBeenCalledWith(initError)
expect(endedSpy).toHaveBeenCalledWith({
code: 1006,
reason: `Couldn't start a new session`,
})
expect(session.status).toBe('ended')
expect(mockCreateSession).not.toHaveBeenCalled()
expect(unhandledRejections).toEqual([])
} finally {
process.off('unhandledRejection', onUnhandledRejection)
}
})
})
50 changes: 33 additions & 17 deletions packages/sdk-js/src/v2/live/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,28 +172,44 @@ export class LiveV2Session {
}

private async startSession(): Promise<void> {
const session = await this.initSessionPromise
this.initSessionResponse = session
try {
const session = await this.initSessionPromise
this.initSessionResponse = session

if (this.abortController.signal.aborted) {
return
}
if (this.abortController.signal.aborted) {
return
}

if (this._status === 'starting') {
this._status = 'started'
this.emit('started', session)
}
if (this._status === 'starting') {
this._status = 'started'
this.emit('started', session)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you using a formatter or have any formatting config in the sdk? Just to make review easier in the future :)

Not blocking

}

if (this.sessionOptions.messages_config?.receive_lifecycle_events) {
const startSessionMessage: LiveV2StartSessionMessage = {
type: 'start_session',
session_id: session.id,
created_at: session.created_at,
if (this.sessionOptions.messages_config?.receive_lifecycle_events) {
const startSessionMessage: LiveV2StartSessionMessage = {
type: 'start_session',
session_id: session.id,
created_at: session.created_at,
}
this.emit('message', startSessionMessage)
}
this.emit('message', startSessionMessage)
}

this.connectToWebSocket(session)
this.connectToWebSocket(session)
} catch (error) {
// initSession already emits 'error' and destroys on failure.
// Catch here so the constructor's fire-and-forget call cannot become an
// unhandled rejection that crashes the Node process.
if (this.abortController.signal.aborted || this._status === 'ended') {
return
}
this.emit(
'error',
error instanceof Error
? error
: new Error(`Error starting session: ${error}`, { cause: error })
)
this.doDestroy(1006, `Couldn't start session`)
}
}

private connectToWebSocket(session: LiveV2InitResponse): void {
Expand Down
Loading