Skip to content

Commit 99ef742

Browse files
neilshwekyp
andcommitted
RUBY-1595 Improve error reporting when symbol/string is passed as read preference (#2582)
* RUBY-1595 Improve error reporting when symbol/string is passed as read preference * tweak wording * RUBY-1595 fix tests Co-authored-by: Oleg Pudeyev <code@olegp.name>
1 parent b9fb789 commit 99ef742

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

lib/mongo/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ def validate_read!(option, opts)
15701570
# for custom classes implementing key access ([]).
15711571
# Instead reject common cases of strings and symbols.
15721572
if read.is_a?(String) || read.is_a?(Symbol)
1573-
raise Error::InvalidReadOption.new(read, 'must be a hash')
1573+
raise Error::InvalidReadOption.new(read, "the read preference must be specified as a hash: { mode: #{read.inspect} }")
15741574
end
15751575

15761576
if mode = read[:mode]

lib/mongo/error/invalid_read_option.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class InvalidReadOption < Error
3131
#
3232
# @since 2.6.0
3333
def initialize(read_option, msg)
34-
super("Invalid read option: #{read_option}: #{msg}")
34+
super("Invalid read preference value: #{read_option.inspect}: #{msg}")
3535
end
3636
end
3737
end

spec/mongo/client_construction_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,28 +1773,28 @@
17731773
expect do
17741774
client = new_local_client_nmio(['127.0.0.1:27017'],
17751775
:read => {:mode => :bogus})
1776-
end.to raise_error(Mongo::Error::InvalidReadOption, 'Invalid read option: {"mode"=>:bogus}: mode bogus is not one of recognized modes')
1776+
end.to raise_error(Mongo::Error::InvalidReadOption, 'Invalid read preference value: {"mode"=>:bogus}: mode bogus is not one of recognized modes')
17771777
end
17781778

17791779
it 'rejects bogus read preference as string' do
17801780
expect do
17811781
client = new_local_client_nmio(['127.0.0.1:27017'],
17821782
:read => {:mode => 'bogus'})
1783-
end.to raise_error(Mongo::Error::InvalidReadOption, 'Invalid read option: {"mode"=>"bogus"}: mode bogus is not one of recognized modes')
1783+
end.to raise_error(Mongo::Error::InvalidReadOption, 'Invalid read preference value: {"mode"=>"bogus"}: mode bogus is not one of recognized modes')
17841784
end
17851785

17861786
it 'rejects read option specified as a string' do
17871787
expect do
17881788
client = new_local_client_nmio(['127.0.0.1:27017'],
17891789
:read => 'primary')
1790-
end.to raise_error(Mongo::Error::InvalidReadOption, 'Invalid read option: primary: must be a hash')
1790+
end.to raise_error(Mongo::Error::InvalidReadOption, 'Invalid read preference value: "primary": the read preference must be specified as a hash: { mode: "primary" }')
17911791
end
17921792

17931793
it 'rejects read option specified as a symbol' do
17941794
expect do
17951795
client = new_local_client_nmio(['127.0.0.1:27017'],
17961796
:read => :primary)
1797-
end.to raise_error(Mongo::Error::InvalidReadOption, 'Invalid read option: primary: must be a hash')
1797+
end.to raise_error(Mongo::Error::InvalidReadOption, "Invalid read preference value: :primary: the read preference must be specified as a hash: { mode: :primary }")
17981798
end
17991799
end
18001800
end

0 commit comments

Comments
 (0)