From f22717a90f66a0a7ed751f653129b18edd5ad0c2 Mon Sep 17 00:00:00 2001 From: Emily Date: Mon, 8 Feb 2016 18:23:13 +0100 Subject: [PATCH 1/2] RUBY-1086 Raise error when reply has cursor_not_found flag set --- lib/mongo/error.rb | 5 +++++ lib/mongo/error/parser.rb | 13 ++++++++++++- lib/mongo/operation/result.rb | 4 ++-- lib/mongo/protocol/reply.rb | 12 ++++++++++++ spec/mongo/operation/result_spec.rb | 15 +++++++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/mongo/error.rb b/lib/mongo/error.rb index c629ec3e2f..028f506d4b 100644 --- a/lib/mongo/error.rb +++ b/lib/mongo/error.rb @@ -62,6 +62,11 @@ class Error < StandardError # # @since 2.0.0 BAD_VALUE = 2.freeze + + # Constant for a Cursor not found error. + # + # @since 2.2.3 + CURSOR_NOT_FOUND = 'Cursor not found.' end end diff --git a/lib/mongo/error/parser.rb b/lib/mongo/error/parser.rb index 6fe177cc52..7a4f40c2b1 100644 --- a/lib/mongo/error/parser.rb +++ b/lib/mongo/error/parser.rb @@ -27,6 +27,9 @@ class Parser # @return [ String ] message The error message parsed from the document. attr_reader :message + # @return [ Array ] replies The message replies. + attr_reader :replies + # Create the new parser with the returned document. # # @example Create the new parser. @@ -35,8 +38,9 @@ class Parser # @param [ BSON::Document ] document The returned document. # # @since 2.0.0 - def initialize(document) + def initialize(document, replies = nil) @document = document || {} + @replies = replies parse! end @@ -50,6 +54,7 @@ def parse! parse_multiple(@message, WRITE_ERRORS) parse_single(@message, ERRMSG, document[WRITE_CONCERN_ERROR]) if document[WRITE_CONCERN_ERROR] + parse_flag(@message) end def parse_single(message, key, doc = document) @@ -66,6 +71,12 @@ def parse_multiple(message, key) end end + def parse_flag(message) + if replies && replies.first && replies.first.cursor_not_found? + append(message ,"#{CURSOR_NOT_FOUND}") + end + end + def append(message, error) if message.length > 1 message.concat(", #{error}") diff --git a/lib/mongo/operation/result.rb b/lib/mongo/operation/result.rb index 99ff0db8f5..7c3599e38d 100644 --- a/lib/mongo/operation/result.rb +++ b/lib/mongo/operation/result.rb @@ -290,7 +290,7 @@ def aggregate_written_count end def parser - @parser ||= Error::Parser.new(first_document) + @parser ||= Error::Parser.new(first_document, replies) end def first_document @@ -298,7 +298,7 @@ def first_document end def query_failure? - replies.first && replies.first.query_failure? + replies.first && (replies.first.query_failure? || replies.first.cursor_not_found?) end end end diff --git a/lib/mongo/protocol/reply.rb b/lib/mongo/protocol/reply.rb index 849d4d850c..271f075a1e 100644 --- a/lib/mongo/protocol/reply.rb +++ b/lib/mongo/protocol/reply.rb @@ -38,6 +38,18 @@ def query_failure? flags.include?(:query_failure) end + # Determine if the reply had a cursor not found flag. + # + # @example Did the reply have a cursor not found flag. + # reply.cursor_not_found? + # + # @return [ true, false ] If the query cursor was not found. + # + # @since 2.2.3 + def cursor_not_found? + flags.include?(:cursor_not_found) + end + # Return the event payload for monitoring. # # @example Return the event payload. diff --git a/spec/mongo/operation/result_spec.rb b/spec/mongo/operation/result_spec.rb index 9c193da627..9e78fb69a5 100644 --- a/spec/mongo/operation/result_spec.rb +++ b/spec/mongo/operation/result_spec.rb @@ -208,6 +208,21 @@ expect(result).to_not be_successful end end + + context 'when the query reply has the cursor_not_found flag set' do + + let(:flags) do + [ :cursor_not_found ] + end + + let(:documents) do + [] + end + + it 'returns false' do + expect(result).to_not be_successful + end + end end context 'when the reply is for a write command' do From 8a610da3b0cfcec9fd696e71298f5284b592a4d8 Mon Sep 17 00:00:00 2001 From: Emily Date: Tue, 9 Feb 2016 13:56:06 +0100 Subject: [PATCH 2/2] No need for String interpolation --- lib/mongo/error/parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongo/error/parser.rb b/lib/mongo/error/parser.rb index 7a4f40c2b1..cf0e1c60e1 100644 --- a/lib/mongo/error/parser.rb +++ b/lib/mongo/error/parser.rb @@ -73,7 +73,7 @@ def parse_multiple(message, key) def parse_flag(message) if replies && replies.first && replies.first.cursor_not_found? - append(message ,"#{CURSOR_NOT_FOUND}") + append(message, CURSOR_NOT_FOUND) end end