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 2 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
19 changes: 14 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,21 @@ function fastifyRedis (fastify, options, next) {
}

if (!redisOptions.lazyConnect) {
return client.info((err, _) => {
return err ? next(err) : next()
})
} else {
next()
const onError = function (err) {
client.quit(() => next(err))
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should quit the client on the first error. In this case we are avoiding the retry strategy that is really important imho.
If we want to go with this event path I think we need to manually handle the retry strategy.

}

const onReady = function () {
client.off('error', onError)
next()
}

return client
.on('ready', onReady)
.on('error', onError)
}

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
14 changes: 11 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ 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
}
this.off = () => {}

return this
}
})
Expand Down Expand Up @@ -377,7 +386,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 +397,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