Skip to content

Commit 124568d

Browse files
authored
RUBY-1340 Provide Transactions example for Docs (#976)
1 parent b43c044 commit 124568d

File tree

29 files changed

+295
-113
lines changed

29 files changed

+295
-113
lines changed

lib/mongo/error.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,43 @@ class Error < StandardError
6969
#
7070
# @since 2.2.3
7171
CURSOR_NOT_FOUND = 'Cursor not found.'
72+
73+
# Error label describing commitTransaction errors that may or may not occur again if a commit is
74+
# manually retried by the user.
75+
#
76+
# @since 2.6.0
77+
UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL = 'UnknownTransactionCommitResult'.freeze
78+
79+
# Error label describing errors that will likely not occur if a transaction is manually retried
80+
# from the start.
81+
#
82+
# @since 2.6.0
83+
TRANSIENT_TRANSACTION_ERROR_LABEL = 'TransientTransactionError'.freeze
84+
85+
def initialize(msg = nil)
86+
@labels = []
87+
super(msg)
88+
end
89+
90+
# Does the error have the given label?
91+
#
92+
# @example
93+
# error.label?(label)
94+
#
95+
# @param [ String ] label The label to check if the error has.
96+
#
97+
# @return [ true, false ] Whether the error has the given label.
98+
#
99+
# @since 2.6.0
100+
def label?(label)
101+
@labels.include?(label)
102+
end
103+
104+
private
105+
106+
def add_label(label)
107+
@labels << label unless label?(label)
108+
end
72109
end
73110
end
74111

lib/mongo/error/no_server_available.rb

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,13 @@ class NoServerAvailable < Error
2727
#
2828
# @param [ Hash ] server_selector The server preference that could not be
2929
# satisfied.
30-
# @param [ Array<String> ] labels A set of labels describing the error.
3130
#
3231
# @since 2.0.0
33-
def initialize(server_selector, labels = nil)
34-
@labels = labels || []
32+
def initialize(server_selector)
3533
super("No server is available matching preference: #{server_selector.inspect} " +
3634
"using server_selection_timeout=#{server_selector.server_selection_timeout} " +
3735
"and local_threshold=#{server_selector.local_threshold}")
3836
end
39-
40-
# Does the error have the given label?
41-
#
42-
# @example
43-
# error.label?(label)
44-
#
45-
# @param [ String ] label The label to check if the error has.
46-
#
47-
# @return [ true, false ] Whether the error has the given label.
48-
#
49-
# @since 2.6.0
50-
def label?(label)
51-
@labels.include?(label)
52-
end
53-
54-
private
55-
56-
def add_label(label)
57-
@labels << label unless label?(label)
58-
end
5937
end
6038
end
6139
end

lib/mongo/error/operation_failure.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,6 @@ def change_stream_resumable_code?
162162
end
163163
private :change_stream_resumable_code?
164164

165-
# Does the error have the given label?
166-
#
167-
# @example
168-
# error.label?(label)
169-
#
170-
# @param [ String ] label The label to check if the error has.
171-
#
172-
# @return [ true, false ] Whether the error has the given label.
173-
#
174-
# @since 2.6.0
175-
def label?(label)
176-
@labels.include?(label) ||
177-
(@result.send(:first_document) && @result.send(:first_document)['errorLabels'])
178-
end
179-
180165
# Create the operation failure.
181166
#
182167
# @example Create the error object
@@ -197,15 +182,8 @@ def initialize(message = nil, result = nil, options = {})
197182
@result = result
198183
@code = options[:code]
199184
@code_name = options[:code_name]
200-
@labels = options[:labels] || []
201185
super(message)
202186
end
203-
204-
private
205-
206-
def add_label(label)
207-
@labels << label unless label?(label)
208-
end
209187
end
210188
end
211189
end

lib/mongo/error/socket_error.rb

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,6 @@ class Error
2020
# @since 2.0.0
2121
class SocketError < Error
2222
include WriteRetryable
23-
24-
# Instantiate the new exception.
25-
#
26-
# @example Instantiate the exception.
27-
# Mongo::Error::SocketError.new(msg)
28-
#
29-
# @param [ String ] msg The error message.
30-
#
31-
# @since 2.0.0
32-
def initialize(msg = nil, labels = nil)
33-
@labels = labels || []
34-
super(msg) if msg
35-
end
36-
37-
# Does the error have the given label?
38-
#
39-
# @example
40-
# error.label?(label)
41-
#
42-
# @return [ true, false ] Whether the error has the given label.
43-
#
44-
# @since 2.6.0
45-
def label?(label)
46-
@labels.include?(label)
47-
end
48-
49-
private
50-
51-
def add_label(label)
52-
@labels << label unless label?(label)
53-
end
5423
end
5524
end
5625
end

lib/mongo/operation/aggregate/op_msg.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def execute(server)
4242
process_result(result, server)
4343
result.validate!
4444
rescue Mongo::Error::SocketError => e
45-
e.send(:add_label, Mongo::Session::TRANSIENT_TRANSACTION_ERROR_LABEL) if session.in_transaction?
45+
e.send(:add_label, Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL) if session.in_transaction?
4646
raise e
4747
end
4848

lib/mongo/operation/create/op_msg.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def execute(server)
3131
process_result(result, server)
3232
result.validate!
3333
rescue Mongo::Error::SocketError => e
34-
e.send(:add_label, Mongo::Session::TRANSIENT_TRANSACTION_ERROR_LABEL) if session.in_transaction?
34+
e.send(:add_label, Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL) if session.in_transaction?
3535
raise e
3636
end
3737

lib/mongo/operation/create_index/op_msg.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def execute(server)
3131
process_result(result, server)
3232
result.validate!
3333
rescue Mongo::Error::SocketError => e
34-
e.send(:add_label, Mongo::Session::TRANSIENT_TRANSACTION_ERROR_LABEL) if session.in_transaction?
34+
e.send(:add_label, Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL) if session.in_transaction?
3535
raise e
3636
end
3737

lib/mongo/operation/create_user/op_msg.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def execute(server)
3131
process_result(result, server)
3232
result.validate!
3333
rescue Mongo::Error::SocketError => e
34-
e.send(:add_label, Mongo::Session::TRANSIENT_TRANSACTION_ERROR_LABEL) if session.in_transaction?
34+
e.send(:add_label, Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL) if session.in_transaction?
3535
raise e
3636
end
3737

lib/mongo/operation/delete/op_msg.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def execute(server)
4141
result = Result.new(dispatch_message(server))
4242
process_result(result, server)
4343
rescue Mongo::Error::SocketError => e
44-
e.send(:add_label, Mongo::Session::TRANSIENT_TRANSACTION_ERROR_LABEL) if session.in_transaction?
44+
e.send(:add_label, Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL) if session.in_transaction?
4545
raise e
4646
end
4747

lib/mongo/operation/distinct/op_msg.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def execute(server)
3232
process_result(result, server)
3333
result.validate!
3434
rescue Mongo::Error::SocketError => e
35-
e.send(:add_label, Mongo::Session::TRANSIENT_TRANSACTION_ERROR_LABEL) if session.in_transaction?
35+
e.send(:add_label, Mongo::Error::TRANSIENT_TRANSACTION_ERROR_LABEL) if session.in_transaction?
3636
raise e
3737
end
3838

0 commit comments

Comments
 (0)