Skip to content

Commit ec58a4e

Browse files
authored
fix: capture websocket server errors (#131)
* fix: capture websocket server errors * add tests
1 parent 328fd6b commit ec58a4e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ function setupWebSocketProxy (fastify, options) {
8888
oldClose.call(this, done)
8989
}
9090

91+
server.on('error', (err) => {
92+
fastify.log.error(err)
93+
})
94+
9195
server.on('connection', (source, request) => {
9296
const url = createWebSocketUrl(options, request)
9397

test/websocket.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,18 @@ test('basic websocket proxy', async (t) => {
5252
server.close()
5353
])
5454
})
55+
56+
test('captures errors on start', async (t) => {
57+
const app = Fastify()
58+
await app.listen(0)
59+
60+
const app2 = Fastify()
61+
app2.register(proxy, { upstream: 'http://localhost', websocket: true })
62+
63+
const appPort = app.server.address().port
64+
65+
await t.rejects(app2.listen(appPort), /EADDRINUSE/)
66+
67+
t.tearDown(app.close.bind(app))
68+
t.tearDown(app2.close.bind(app2))
69+
})

0 commit comments

Comments
 (0)