Skip to content

Commit 6df1544

Browse files
valerio-pizzichinizekthclimba03003
authored
fix: added connection error handling (#98)
* fix: added connection error handling * doc: added connection error handling doc * doc: fix README typos Co-authored-by: Vincent LE GOFF <[email protected]> * doc: fix README typos Co-authored-by: Vincent LE GOFF <[email protected]> * refactor: added error propagation from ioredis - added tests for connection with password * doc: removed error examples on error handling sections * fix: added redis image tag on ci workflow * rafactor: generalize error inside tests * feat: added matrix on redis tags (test) * refactor: add space after comma Co-authored-by: KaKa <[email protected]> Co-authored-by: Vincent LE GOFF <[email protected]> Co-authored-by: KaKa <[email protected]>
1 parent b1f77eb commit 6df1544

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ jobs:
1717
strategy:
1818
matrix:
1919
node-version: [10, 12, 14, 16]
20+
redis-tag: [5, 6]
2021
services:
2122
redis:
22-
image: redis
23+
image: redis:${{ matrix.redis-tag }}
2324
ports:
2425
- 6379:6379
2526
options: --entrypoint redis-server

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ fastify.listen(3000, function (err) {
186186
```
187187
*NB you can find more information about Redis streams and the relevant commands [here](https://redis.io/topics/streams-intro) and [here](https://redis.io/commands#stream).*
188188

189+
## Redis connection error
190+
Majority of errors are silent due to the `ioredis` silent error handling but during the plugin registration it will check that the connection with the redis instance is correctly estabilished.
191+
In this case you can receive an `ERR_AVVIO_PLUGIN_TIMEOUT` error if the connection can't be estabilished in the expected time frame or a dedicated error for an invalid connection.
192+
189193
## Acknowledgements
190194

191195
This project is kindly sponsored by:

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ function fastifyRedis (fastify, options, next) {
6464
}
6565
}
6666

67-
next()
67+
if (!redisOptions.lazyConnect) {
68+
return client.info((err, _) => {
69+
return err ? next(err) : next()
70+
})
71+
} else {
72+
next()
73+
}
6874
}
6975

7076
function close (fastify, done) {

test/test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,35 @@ const test = t.test
66
const Fastify = require('fastify')
77
const fastifyRedis = require('..')
88

9+
const TEST_PASSWORD = 'my_secret_password'
10+
11+
const setRedisPassword = async (password) => {
12+
const fastify = Fastify()
13+
14+
fastify.register(fastifyRedis, {
15+
host: '127.0.0.1'
16+
})
17+
18+
await fastify.ready()
19+
await fastify.redis.flushall()
20+
await fastify.redis.config(['set', 'requirepass', password])
21+
await fastify.close()
22+
}
23+
24+
const unsetRedisPassword = async (currentPassword) => {
25+
const fastify = Fastify()
26+
27+
fastify.register(fastifyRedis, {
28+
host: '127.0.0.1',
29+
password: currentPassword
30+
})
31+
32+
await fastify.ready()
33+
await fastify.redis.flushall()
34+
await fastify.redis.config(['set', 'requirepass', ''])
35+
await fastify.close()
36+
}
37+
938
t.beforeEach(async () => {
1039
const fastify = Fastify()
1140

@@ -44,6 +73,7 @@ test('fastify.redis should support url', (t) => {
4473
otherOption: 'foo'
4574
})
4675
this.quit = () => {}
76+
this.info = cb => cb(null, 'info')
4777
return this
4878
}
4979
})
@@ -345,3 +375,80 @@ test('Should not throw within different contexts', (t) => {
345375
t.error(error)
346376
})
347377
})
378+
379+
test('Should throw when trying to connect on an invalid host', (t) => {
380+
t.plan(2)
381+
382+
const fastify = Fastify({ pluginTimeout: 20000 })
383+
t.teardown(() => fastify.close())
384+
385+
fastify
386+
.register(fastifyRedis, {
387+
host: 'invalid_host'
388+
})
389+
390+
fastify.ready((err) => {
391+
t.type(err, 'MaxRetriesPerRequestError')
392+
t.equal(err.message, 'Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.')
393+
})
394+
})
395+
396+
test('Should not throw when trying to connect on an invalid host but the lazyConnect option has been provided', (t) => {
397+
t.plan(1)
398+
399+
const fastify = Fastify()
400+
t.teardown(() => fastify.close())
401+
402+
fastify
403+
.register(fastifyRedis, {
404+
host: 'invalid_host',
405+
lazyConnect: true
406+
})
407+
408+
fastify.ready((err) => {
409+
t.error(err)
410+
})
411+
})
412+
413+
test('Should throw authentication error when trying to connect on a valid host with a wrong password', (t) => {
414+
t.plan(1)
415+
416+
const fastify = Fastify()
417+
t.teardown(async () => {
418+
fastify.close()
419+
await unsetRedisPassword(TEST_PASSWORD)
420+
})
421+
422+
setRedisPassword(TEST_PASSWORD)
423+
.then(_ => {
424+
fastify.register(fastifyRedis, {
425+
host: '127.0.0.1',
426+
password: 'my_wrong_secret_password'
427+
})
428+
429+
fastify.ready(err => {
430+
t.ok(err)
431+
})
432+
})
433+
})
434+
435+
test('Should throw authentication error when trying to connect on a valid host without a password', (t) => {
436+
t.plan(1)
437+
438+
const fastify = Fastify()
439+
t.teardown(async () => {
440+
fastify.close()
441+
await unsetRedisPassword(TEST_PASSWORD)
442+
})
443+
444+
setRedisPassword(TEST_PASSWORD)
445+
.then(_ => {
446+
fastify.register(fastifyRedis, {
447+
host: '127.0.0.1'
448+
})
449+
450+
fastify.ready(err => {
451+
t.ok(err)
452+
})
453+
})
454+
})

0 commit comments

Comments
 (0)