Skip to content

Commit 6d44dec

Browse files
kevinten10claude
andcommitted
fix: log WebSocket errors that occur after authentication
Previously, errors after auth were silently swallowed. Now they are logged to console.error for debugging. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 632136a commit 6d44dec

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/ha/ws-client.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,27 @@ describe('HaWsClient', () => {
199199
client.close()
200200
})
201201

202+
it('should log errors that occur after authentication', async () => {
203+
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
204+
205+
const client = new HaWsClient('http://ha.local:8123', 'valid-token')
206+
const connectPromise = client.connect()
207+
208+
await vi.advanceTimersByTimeAsync(10)
209+
await connectPromise
210+
211+
// Simulate a post-auth error
212+
mockWsInstances[0].emit('error', new Error('ECONNRESET'))
213+
214+
expect(spy).toHaveBeenCalledWith(
215+
'[casa-ws] WebSocket error:',
216+
'ECONNRESET',
217+
)
218+
219+
spy.mockRestore()
220+
client.close()
221+
})
222+
202223
it('should not reconnect after explicit close', async () => {
203224
const client = new HaWsClient('http://ha.local:8123', 'valid-token')
204225
const connectPromise = client.connect()

src/ha/ws-client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export class HaWsClient {
7676
this.ws.on('error', (err) => {
7777
if (!this.authenticated) {
7878
reject(err)
79+
} else {
80+
console.error('[casa-ws] WebSocket error:', err.message)
7981
}
8082
})
8183
})

0 commit comments

Comments
 (0)