Skip to content

Commit a562740

Browse files
committed
Merge pull request #748 from estolfo/RUBY-1086-cursor-not-found
RUBY-1086 Raise error when reply has cursor_not_found flag set
2 parents fa9384e + 8a610da commit a562740

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

lib/mongo/error.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ class Error < StandardError
6262
#
6363
# @since 2.0.0
6464
BAD_VALUE = 2.freeze
65+
66+
# Constant for a Cursor not found error.
67+
#
68+
# @since 2.2.3
69+
CURSOR_NOT_FOUND = 'Cursor not found.'
6570
end
6671
end
6772

lib/mongo/error/parser.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class Parser
2727
# @return [ String ] message The error message parsed from the document.
2828
attr_reader :message
2929

30+
# @return [ Array<Protocol::Reply> ] replies The message replies.
31+
attr_reader :replies
32+
3033
# Create the new parser with the returned document.
3134
#
3235
# @example Create the new parser.
@@ -35,8 +38,9 @@ class Parser
3538
# @param [ BSON::Document ] document The returned document.
3639
#
3740
# @since 2.0.0
38-
def initialize(document)
41+
def initialize(document, replies = nil)
3942
@document = document || {}
43+
@replies = replies
4044
parse!
4145
end
4246

@@ -50,6 +54,7 @@ def parse!
5054
parse_multiple(@message, WRITE_ERRORS)
5155
parse_single(@message, ERRMSG,
5256
document[WRITE_CONCERN_ERROR]) if document[WRITE_CONCERN_ERROR]
57+
parse_flag(@message)
5358
end
5459

5560
def parse_single(message, key, doc = document)
@@ -66,6 +71,12 @@ def parse_multiple(message, key)
6671
end
6772
end
6873

74+
def parse_flag(message)
75+
if replies && replies.first && replies.first.cursor_not_found?
76+
append(message, CURSOR_NOT_FOUND)
77+
end
78+
end
79+
6980
def append(message, error)
7081
if message.length > 1
7182
message.concat(", #{error}")

lib/mongo/operation/result.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,15 @@ def aggregate_written_count
290290
end
291291

292292
def parser
293-
@parser ||= Error::Parser.new(first_document)
293+
@parser ||= Error::Parser.new(first_document, replies)
294294
end
295295

296296
def first_document
297297
@first_document ||= first || BSON::Document.new
298298
end
299299

300300
def query_failure?
301-
replies.first && replies.first.query_failure?
301+
replies.first && (replies.first.query_failure? || replies.first.cursor_not_found?)
302302
end
303303
end
304304
end

lib/mongo/protocol/reply.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ def query_failure?
3838
flags.include?(:query_failure)
3939
end
4040

41+
# Determine if the reply had a cursor not found flag.
42+
#
43+
# @example Did the reply have a cursor not found flag.
44+
# reply.cursor_not_found?
45+
#
46+
# @return [ true, false ] If the query cursor was not found.
47+
#
48+
# @since 2.2.3
49+
def cursor_not_found?
50+
flags.include?(:cursor_not_found)
51+
end
52+
4153
# Return the event payload for monitoring.
4254
#
4355
# @example Return the event payload.

spec/mongo/operation/result_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,21 @@
208208
expect(result).to_not be_successful
209209
end
210210
end
211+
212+
context 'when the query reply has the cursor_not_found flag set' do
213+
214+
let(:flags) do
215+
[ :cursor_not_found ]
216+
end
217+
218+
let(:documents) do
219+
[]
220+
end
221+
222+
it 'returns false' do
223+
expect(result).to_not be_successful
224+
end
225+
end
211226
end
212227

213228
context 'when the reply is for a write command' do

0 commit comments

Comments
 (0)