Skip to content

Commit

Permalink
RUBY-1873 Directly reference write concern error fields (mongodb#1428)
Browse files Browse the repository at this point in the history
  • Loading branch information
p-mongo authored and p committed Jul 9, 2019
1 parent f0e13e0 commit d5a8f0c
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/mongo/bulk_write/result.rb
Expand Up @@ -180,7 +180,7 @@ def upserted_ids
#
# @since 2.1.0
def validate!
if @results[Error::WRITE_ERRORS] || @results[Error::WRITE_CONCERN_ERRORS]
if @results['writeErrors'] || @results['writeConcernErrors']
raise Error::BulkWriteError.new(@results)
else
self
Expand Down
4 changes: 2 additions & 2 deletions lib/mongo/bulk_write/result_combiner.rb
Expand Up @@ -112,7 +112,7 @@ def combine_errors!(result)
def combine_write_errors!(result)
if write_errors = result.aggregate_write_errors(count)
results.merge!(
Error::WRITE_ERRORS => ((results[Error::WRITE_ERRORS] || []) << write_errors).flatten
'writeErrors' => ((results['writeErrors'] || []) << write_errors).flatten
)
else
result.validate!
Expand All @@ -121,7 +121,7 @@ def combine_write_errors!(result)

def combine_write_concern_errors!(result)
if write_concern_errors = result.aggregate_write_concern_errors(count)
results[Error::WRITE_CONCERN_ERRORS] = (results[Error::WRITE_CONCERN_ERRORS] || []) +
results['writeConcernErrors'] = (results['writeConcernErrors'] || []) +
write_concern_errors
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/mongo/error.rb
Expand Up @@ -41,16 +41,19 @@ class Error < StandardError
# The constant for the writeErrors array.
#
# @since 2.0.0
# @deprecated
WRITE_ERRORS = 'writeErrors'.freeze

# The constant for a write concern error.
#
# @since 2.0.0
# @deprecated
WRITE_CONCERN_ERROR = 'writeConcernError'.freeze

# The constant for write concern errors.
#
# @since 2.1.0
# @deprecated
WRITE_CONCERN_ERRORS = 'writeConcernErrors'.freeze

# Constant for an unknown error.
Expand Down
6 changes: 3 additions & 3 deletions lib/mongo/error/parser.rb
Expand Up @@ -133,15 +133,15 @@ def write_concern_error_code_name
private

def write_concern_error_document
document[WRITE_CONCERN_ERROR]
document['writeConcernError']
end

def parse!
@message = ""
parse_single(@message, ERR)
parse_single(@message, ERROR)
parse_single(@message, ERRMSG)
parse_multiple(@message, WRITE_ERRORS)
parse_multiple(@message, 'writeErrors')
if write_concern_error_document
parse_single(@message, ERRMSG, write_concern_error_document)
end
Expand Down Expand Up @@ -202,7 +202,7 @@ def parse_code
if @code.nil? && @code_name.nil?
# If we have writeErrors, and all of their codes are the same,
# use that code. Otherwise don't set the code
if write_errors = document[WRITE_ERRORS]
if write_errors = document['writeErrors']
codes = write_errors.map { |e| e['code'] }.compact
if codes.uniq.length == 1
@code = codes.first
Expand Down
4 changes: 2 additions & 2 deletions lib/mongo/operation/shared/result/aggregatable.rb
Expand Up @@ -34,7 +34,7 @@ module Aggregatable
def aggregate_write_errors(count)
return unless @replies
@replies.reduce(nil) do |errors, reply|
if write_errors = reply.documents.first[Error::WRITE_ERRORS]
if write_errors = reply.documents.first['writeErrors']
wes = write_errors.collect do |we|
we.merge!('index' => count + we['index'])
end
Expand All @@ -56,7 +56,7 @@ def aggregate_write_errors(count)
def aggregate_write_concern_errors(count)
return unless @replies
@replies.each_with_index.reduce(nil) do |errors, (reply, _)|
if write_concern_errors = reply.documents.first[Error::WRITE_CONCERN_ERRORS]
if write_concern_errors = reply.documents.first['writeConcernErrors']
(errors || []) << write_concern_errors.reduce(nil) do |errs, wce|
wce.merge!('index' => count + wce['index'])
(errs || []) << write_concern_error
Expand Down
4 changes: 2 additions & 2 deletions spec/mongo/bulk_write_spec.rb
Expand Up @@ -80,7 +80,7 @@
end

it 'sets the document index on the error' do
expect(error.result[Mongo::Error::WRITE_ERRORS].first['index']).to eq(2)
expect(error.result['writeErrors'].first['index']).to eq(2)
end

context 'when a session is provided' do
Expand Down Expand Up @@ -1848,7 +1848,7 @@

it 'sets the document index on the error' do
requests.push({ insert_one: { _id: 5 }})
expect(error.result[Mongo::Error::WRITE_ERRORS].first['index']).to eq(batch_size)
expect(error.result['writeErrors'].first['index']).to eq(batch_size)
end
end

Expand Down

0 comments on commit d5a8f0c

Please sign in to comment.