File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -103,6 +103,20 @@ def self.transaction(isolation = "mvcc")
103
103
end
104
104
end
105
105
106
+ # Rescue a duplicate key exception in the given block. Returns the result of
107
+ # the block, or +false+ if the exception was raised.
108
+ def self . rescue_duplicate_key_errors
109
+ yield
110
+ rescue Minidoc ::DuplicateKey
111
+ false
112
+ rescue Mongo ::OperationFailure => ex
113
+ if Minidoc ::DuplicateKey . duplicate_key_exception ( ex )
114
+ false
115
+ else
116
+ raise
117
+ end
118
+ end
119
+
106
120
def self . tokumx?
107
121
@server_info ||= connection . server_info
108
122
@server_info . key? ( "tokumxVersion" )
Original file line number Diff line number Diff line change
1
+ require File . expand_path ( '../helper' , __FILE__ )
2
+
3
+ class DuplicateKeyTest < Minidoc ::TestCase
4
+ def test_rescue_duplicate_key_errors_result
5
+ user = User . create!
6
+ result = Minidoc . rescue_duplicate_key_errors do
7
+ User . create! ( name : "two" )
8
+ end
9
+
10
+ assert_equal "two" , result . name
11
+ end
12
+
13
+ def test_rescue_duplicate_key_errors_false
14
+ user = User . create!
15
+ result = Minidoc . rescue_duplicate_key_errors do
16
+ User . create! ( _id : user . id , name : "two" )
17
+ end
18
+
19
+ assert_equal false , result
20
+ end
21
+ end
You can’t perform that action at this time.
0 commit comments