Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling #100

Merged
merged 5 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ function fastifyRedis (fastify, options, next) {
}

if (!redisOptions.lazyConnect) {
return client.info((err, _) => {
return err ? next(err) : next()
})
} else {
next()
return client
.on('ready', next)
.on('error', (err) => {
client.quit(() => next(err))
})
}

next()

This comment was marked as resolved.

Copy link
Member

@climba03003 climba03003 Jul 13, 2021

Choose a reason for hiding this comment

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

When specified lazyConnect, the if clause will stop at return client and next is not reachable.

}

function close (fastify, done) {
Expand Down
12 changes: 9 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ test('fastify.redis should support url', (t) => {
})
this.quit = () => {}
this.info = cb => cb(null, 'info')
this.on = function (name, handler) {
if (name === 'ready') {
handler(null, 'ready')
}

return this
}
return this
}
})
Expand Down Expand Up @@ -377,7 +384,7 @@ test('Should not throw within different contexts', (t) => {
})

test('Should throw when trying to connect on an invalid host', (t) => {
t.plan(2)
t.plan(1)

const fastify = Fastify({ pluginTimeout: 20000 })
t.teardown(() => fastify.close())
Expand All @@ -388,8 +395,7 @@ test('Should throw when trying to connect on an invalid host', (t) => {
})

fastify.ready((err) => {
t.type(err, 'MaxRetriesPerRequestError')
t.equal(err.message, 'Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.')
t.ok(err)
})
})

Expand Down