From f621fefca590bdce91a08f0db6d27219ae060658 Mon Sep 17 00:00:00 2001 From: Metallion Date: Mon, 30 Oct 2023 15:29:49 +0900 Subject: [PATCH] Include the error messages when we fail due to too many errors. --- lib/redlock/client.rb | 3 ++- spec/client_spec.rb | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/redlock/client.rb b/lib/redlock/client.rb index e829711..24be410 100644 --- a/lib/redlock/client.rb +++ b/lib/redlock/client.rb @@ -319,7 +319,8 @@ def lock_instances(resource, ttl, options) @servers.each { |s| s.unlock(resource, value) } if errors.size >= @quorum - raise LockAcquisitionError.new('Too many Redis errors prevented lock acquisition', errors) + err_msgs = errors.map { |e| "#{e.class}: #{e.message}" }.join("\n") + raise LockAcquisitionError.new("Too many Redis errors prevented lock acquisition:\n#{err_msgs}", errors) end false diff --git a/spec/client_spec.rb b/spec/client_spec.rb index cbefc40..d46f6e5 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -108,9 +108,19 @@ it 'fails to acquire a lock if majority of Redis instances are not available' do redlock = Redlock::Client.new(servers_without_quorum) + expected_msg = <<~MSG + failed to acquire lock on 'Too many Redis errors prevented lock acquisition: + RedisClient::CannotConnectError: Connection refused - connect(2) for 127.0.0.1:46864 + RedisClient::CannotConnectError: Connection refused - connect(2) for 127.0.0.1:46864' + MSG + expect { redlock.lock(resource_key, ttl) - }.to raise_error(Redlock::LockAcquisitionError) + }.to raise_error do |error| + expect(error).to be_a(Redlock::LockAcquisitionError) + expect(error.message).to eq(expected_msg.chomp) + expect(error.errors.size).to eq(2) + end end end