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

if (!redisOptions.lazyConnect) {
return client.info((err, _) => {
return err ? next(err) : next()
})
} else {
next()
const onEnd = function (err) {
client
.off('ready', onReady)
.off('error', onError)
.off('end', onEnd)
.quit(() => next(err))
}

const onReady = function () {
client
.off('end', onEnd)
.off('error', onError)
.off('ready', onReady)

next()
}

const onError = function (err) {
// Swallow network errors to allow ioredis
// to preform reconnection and emit 'end'
// event if reconnection eventually
// fails.
// Any other errors during startup will
// trigger the 'end' event.
if (err instanceof Redis.ReplyError) {
this.emit('end', err)
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer if we did not call emit() on an object we do not maintain. Just call the onEnd() function.

}
}

client
.on('end', onEnd)
.on('error', onError)
.on('ready', onReady)

return
}

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 = function () { return this }

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