Skip to content

Commit b80d178

Browse files
committed
Merge pull request #552 from estolfo/RUBY-842-error-code
RUBY-842 populate error code when using write commands
2 parents 54e592b + 723f122 commit b80d178

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/mongo/db.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,9 @@ def command(selector, opts={})
589589
end.join('; ')
590590
message << ').'
591591
code = result['code'] || result['assertionCode']
592+
if result['writeErrors']
593+
code = result['writeErrors'].first['code']
594+
end
592595
raise ExecutionTimeout.new(message, code, result) if code == MAX_TIME_MS_CODE
593596
raise OperationFailure.new(message, code, result)
594597
end

test/functional/collection_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,18 @@ def test_update_write_command_with_error
345345
end
346346
end
347347

348+
def test_error_code
349+
coll = @db['test-error-code']
350+
coll.ensure_index(BSON::OrderedHash[:x, Mongo::ASCENDING], { :unique => true })
351+
coll.save(:x => 2)
352+
begin
353+
coll.save(:x => 2)
354+
rescue => ex
355+
assert_not_nil ex.error_code
356+
end
357+
coll.drop
358+
end
359+
348360
def test_aggregation_cursor
349361
return unless @version >= '2.5.1'
350362
[10, 1000].each do |size|

0 commit comments

Comments
 (0)