Skip to content

Commit fa56ab3

Browse files
committed
Improve error log message output
Previously when an error were received by a Redis server, it was not clear whether this came from a Redis Sentinel or a Redis server in the cluster. This commit adds the hostname/port of the server to the exception message via a log context. Closes #177
1 parent dfef587 commit fa56ab3

File tree

6 files changed

+36
-7
lines changed

6 files changed

+36
-7
lines changed

hiredis-client/lib/redis_client/hiredis_connection.rb

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def initialize(ca_file: nil, ca_path: nil, cert: nil, key: nil, hostname: nil)
3939
end
4040
end
4141

42+
attr_reader :config
43+
4244
def initialize(config, connect_timeout:, read_timeout:, write_timeout:)
4345
super()
4446
@config = config

lib/redis_client.rb

+18-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,23 @@ def timeout=(timeout)
7777
end
7878
end
7979

80-
Error = Class.new(StandardError)
80+
module HasConfig
81+
attr_reader :config
82+
83+
def _set_config(config)
84+
@config = config
85+
end
86+
87+
def message
88+
return super unless config
89+
90+
"#{super} (#{config.server_url})"
91+
end
92+
end
93+
94+
class Error < StandardError
95+
include HasConfig
96+
end
8197

8298
ProtocolError = Class.new(Error)
8399
UnsupportedServer = Class.new(Error)
@@ -114,7 +130,7 @@ def parse(error_message)
114130
end
115131
code ||= error_message.split(' ', 2).first
116132
klass = ERRORS.fetch(code, self)
117-
klass.new(error_message)
133+
klass.new(error_message.strip)
118134
end
119135
end
120136
end

lib/redis_client/connection_mixin.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def call(command, timeout)
3232
@pending_reads -= 1
3333
if result.is_a?(Error)
3434
result._set_command(command)
35+
result._set_config(config)
3536
raise result
3637
else
3738
result
@@ -53,10 +54,16 @@ def call_pipelined(commands, timeouts)
5354

5455
# A multi/exec command can return an array of results.
5556
# An error from a multi/exec command is handled in Multi#_coerce!.
56-
if result.is_a?(Error)
57+
if result.is_a?(Array)
58+
result.each do |res|
59+
res._set_config(config) if res.is_a?(Error)
60+
end
61+
elsif result.is_a?(Error)
5762
result._set_command(commands[index])
63+
result._set_config(config)
5864
exception ||= result
5965
end
66+
6067
results[index] = result
6168
end
6269

lib/redis_client/ruby_connection.rb

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def ssl_context(ssl_params)
4040

4141
SUPPORTS_RESOLV_TIMEOUT = Socket.method(:tcp).parameters.any? { |p| p.last == :resolv_timeout }
4242

43+
attr_reader :config
44+
4345
def initialize(config, connect_timeout:, read_timeout:, write_timeout:)
4446
super()
4547
@config = config

test/redis_client_test.rb

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def call_pipelined(commands, *)
6464
client.call("PING")
6565
end
6666
assert_includes error.message, "redis-client requires Redis 6+ with HELLO command available"
67+
assert_includes error.message, "(redis://"
6768
end
6869

6970
def test_redis_6_server_with_missing_hello_command
@@ -82,6 +83,7 @@ def call_pipelined(commands, *)
8283
client.call("PING")
8384
end
8485
assert_includes error.message, "redis-client requires Redis 6+ with HELLO command available"
86+
assert_includes error.message, "(redis://"
8587
end
8688

8789
def test_handle_async_raise

test/shared/redis_client_tests.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -271,30 +271,30 @@ def test_wrong_type
271271
@redis.call("SISMEMBER", "str", "member")
272272
end
273273
assert_equal ["SISMEMBER", "str", "member"], error.command
274-
assert_includes error.message, "WRONGTYPE Operation against a key holding the wrong kind of value"
274+
assert_match(/WRONGTYPE Operation against a key holding the wrong kind of value (.*:.*)/, error.message)
275275

276276
error = assert_raises RedisClient::CommandError do
277277
@redis.pipelined do |pipeline|
278278
pipeline.call("SISMEMBER", "str", "member")
279279
end
280280
end
281281
assert_equal ["SISMEMBER", "str", "member"], error.command
282-
assert_includes error.message, "WRONGTYPE Operation against a key holding the wrong kind of value"
282+
assert_match(/WRONGTYPE Operation against a key holding the wrong kind of value (.*:.*)/, error.message)
283283

284284
error = assert_raises RedisClient::CommandError do
285285
@redis.multi do |transaction|
286286
transaction.call("SISMEMBER", "str", "member")
287287
end
288288
end
289289
assert_equal ["SISMEMBER", "str", "member"], error.command
290-
assert_includes error.message, "WRONGTYPE Operation against a key holding the wrong kind of value"
290+
assert_match(/WRONGTYPE Operation against a key holding the wrong kind of value (.*:.*)/, error.message)
291291
end
292292

293293
def test_command_missing
294294
error = assert_raises RedisClient::CommandError do
295295
@redis.call("DOESNOTEXIST", "foo")
296296
end
297-
assert_match(/ERR unknown command .DOESNOTEXIST./, error.message)
297+
assert_match(/ERR unknown command .DOESNOTEXIST.*\(.*:.*\)/, error.message)
298298
end
299299

300300
def test_authentication

0 commit comments

Comments
 (0)