|
| 1 | +# Copyright (C) 2019 MongoDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +module Mongo |
| 16 | + module Operation |
| 17 | + |
| 18 | + # Shared behavior of response handling for operations. |
| 19 | + # |
| 20 | + # @api private |
| 21 | + module ResponseHandling |
| 22 | + |
| 23 | + private |
| 24 | + |
| 25 | + def validate_result(result) |
| 26 | + unpin_maybe(session) do |
| 27 | + add_error_labels do |
| 28 | + result.validate! |
| 29 | + end |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + # Adds error labels to exceptions raised in the yielded to block, |
| 34 | + # which should perform MongoDB operations and raise Mongo::Errors on |
| 35 | + # failure. This method handles network errors (Error::SocketError) |
| 36 | + # and server-side errors (Error::OperationFailure); it does not |
| 37 | + # handle server selection errors (Error::NoServerAvailable), for which |
| 38 | + # labels are added in the server selection code. |
| 39 | + def add_error_labels |
| 40 | + begin |
| 41 | + yield |
| 42 | + rescue Mongo::Error::SocketError => e |
| 43 | + if session && session.in_transaction? && !session.committing_transaction? |
| 44 | + e.add_label('TransientTransactionError') |
| 45 | + end |
| 46 | + if session && session.committing_transaction? |
| 47 | + e.add_label('UnknownTransactionCommitResult') |
| 48 | + end |
| 49 | + raise e |
| 50 | + rescue Mongo::Error::OperationFailure => e |
| 51 | + if session && session.committing_transaction? |
| 52 | + if e.write_retryable? || e.wtimeout? || (e.write_concern_error? && |
| 53 | + !Session::UNLABELED_WRITE_CONCERN_CODES.include?(e.write_concern_error_code) |
| 54 | + ) || e.max_time_ms_expired? |
| 55 | + e.add_label('UnknownTransactionCommitResult') |
| 56 | + end |
| 57 | + end |
| 58 | + raise e |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + # Unpins the session if the session is pinned and the yielded to block |
| 63 | + # raises errors that are required to unpin the session. |
| 64 | + # |
| 65 | + # @note This method takes the session as an argument because this module |
| 66 | + # is included in BulkWrite which does not store the session in the |
| 67 | + # receiver (despite Specifiable doing so). |
| 68 | + # |
| 69 | + # @param [ Session | nil ] Session to consider. |
| 70 | + def unpin_maybe(session) |
| 71 | + yield |
| 72 | + rescue Mongo::Error => e |
| 73 | + if session |
| 74 | + session.unpin_maybe(e) |
| 75 | + end |
| 76 | + raise |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | +end |
0 commit comments