Module: Mongo::Error::OperationFailure::Family

Extended by:
Forwardable
Includes:
ReadWriteRetryable, SdamErrorDetection
Included in:
Mongo::Error::OperationFailure, ServerTimeoutError
Defined in:
lib/mongo/error/operation_failure.rb

Overview

Implements the behavior for an OperationFailure error. Other errors (e.g. ServerTimeoutError) may also implement this, so that they may be recognized and treated as OperationFailure errors.

Since:

  • 2.0.0

Constant Summary collapse

CHANGE_STREAM_RESUME_ERRORS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Error codes and code names that should result in a failing getMore command on a change stream NOT being resumed.

Since:

  • 2.0.0

[
  { code_name: 'HostUnreachable', code: 6 },
  { code_name: 'HostNotFound', code: 7 },
  { code_name: 'NetworkTimeout', code: 89 },
  { code_name: 'ShutdownInProgress', code: 91 },
  { code_name: 'PrimarySteppedDown', code: 189 },
  { code_name: 'ExceededTimeLimit', code: 262 },
  { code_name: 'SocketException', code: 9001 },
  { code_name: 'NotMaster', code: 10_107 },
  { code_name: 'InterruptedAtShutdown', code: 11_600 },
  { code_name: 'InterruptedDueToReplStateChange', code: 11_602 },
  { code_name: 'NotPrimaryNoSecondaryOk', code: 13_435 },
  { code_name: 'NotMasterOrSecondary', code: 13_436 },

  { code_name: 'StaleShardVersion', code: 63 },
  { code_name: 'FailedToSatisfyReadPreference', code: 133 },
  { code_name: 'StaleEpoch', code: 150 },
  { code_name: 'RetryChangeStream', code: 234 },
  { code_name: 'StaleConfig', code: 13_388 },
].freeze
CHANGE_STREAM_RESUME_MESSAGES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Change stream can be resumed when these error messages are encountered.

Since:

  • 2.6.0

ReadWriteRetryable::WRITE_RETRY_MESSAGES

Constants included from ReadWriteRetryable

ReadWriteRetryable::RETRY_MESSAGES, ReadWriteRetryable::WRITE_RETRY_ERRORS, ReadWriteRetryable::WRITE_RETRY_MESSAGES

Constants included from SdamErrorDetection

SdamErrorDetection::NODE_RECOVERING_CODES, SdamErrorDetection::NODE_SHUTTING_DOWN_CODES, SdamErrorDetection::NOT_MASTER_CODES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ReadWriteRetryable

#retryable?, #write_retryable?

Methods included from SdamErrorDetection

#node_recovering?, #node_shutting_down?, #not_master?

Instance Attribute Details

#codeInteger (readonly)

Returns The error code parsed from the document.

Returns:

  • (Integer)

    The error code parsed from the document.

Since:

  • 2.6.0



43
44
45
# File 'lib/mongo/error/operation_failure.rb', line 43

def code
  @code
end

#code_nameString (readonly)

Returns The error code name parsed from the document.

Returns:

  • (String)

    The error code name parsed from the document.

Since:

  • 2.6.0



48
49
50
# File 'lib/mongo/error/operation_failure.rb', line 48

def code_name
  @code_name
end

#detailsString | nil (readonly)

Returns The details of the error. For WriteConcernErrors this is ‘document[’errInfo’]‘. For WriteErrors this is `document[0]`. For all other errors this is nil.

Returns:

  • (String | nil)

    The details of the error. For WriteConcernErrors this is ‘document[’errInfo’]‘. For WriteErrors this is `document[0]`. For all other errors this is nil.

Since:

  • 2.0.0



155
156
157
# File 'lib/mongo/error/operation_failure.rb', line 155

def details
  @details
end

#documentBSON::Document | nil (readonly)

Returns The server-returned error document.

Returns:

  • (BSON::Document | nil)

    The server-returned error document.

Since:

  • 2.0.0



160
161
162
# File 'lib/mongo/error/operation_failure.rb', line 160

def document
  @document
end

#resultOperation::Result (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the result object for the operation.

Returns:

Since:

  • 2.0.0



165
166
167
# File 'lib/mongo/error/operation_failure.rb', line 165

def result
  @result
end

#server_addressString | nil (readonly)

Returns The address (“host:port”) of the server that produced this error, if known.

Returns:

  • (String | nil)

    The address (“host:port”) of the server that produced this error, if known.

Since:

  • 2.0.0



58
59
60
# File 'lib/mongo/error/operation_failure.rb', line 58

def server_address
  @server_address
end

#server_messageString (readonly)

Returns The server-returned error message parsed from the response.

Returns:

  • (String)

    The server-returned error message parsed from the response.

Since:

  • 2.0.0



54
55
56
# File 'lib/mongo/error/operation_failure.rb', line 54

def server_message
  @server_message
end

#write_concern_error_codeInteger | nil (readonly)

Returns The error code for the write concern error, if a write concern error is present and has a code.

Returns:

  • (Integer | nil)

    The error code for the write concern error, if a write concern error is present and has a code.

Since:

  • 2.10.0



143
144
145
# File 'lib/mongo/error/operation_failure.rb', line 143

def write_concern_error_code
  @write_concern_error_code
end

#write_concern_error_code_nameString | nil (readonly)

Returns The code name for the write concern error, if a write concern error is present and has a code name.

Returns:

  • (String | nil)

    The code name for the write concern error, if a write concern error is present and has a code name.

Since:

  • 2.10.0



149
150
151
# File 'lib/mongo/error/operation_failure.rb', line 149

def write_concern_error_code_name
  @write_concern_error_code_name
end

#write_concern_error_documentHash | nil (readonly)

Returns the write concern error document as it was reported by the server, if any.

Returns:

  • (Hash | nil)

    Write concern error as reported to the server.

Since:

  • 2.0.0



137
138
139
# File 'lib/mongo/error/operation_failure.rb', line 137

def write_concern_error_document
  @write_concern_error_document
end

Instance Method Details

#change_stream_resumable?true, false

Can the change stream on which this error occurred be resumed, provided the operation that triggered this error was a getMore?

Examples:

Is the error resumable for the change stream?

error.change_stream_resumable?

Returns:

  • (true, false)

    Whether the error is resumable.

Since:

  • 2.6.0



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mongo/error/operation_failure.rb', line 100

def change_stream_resumable?
  if @result && @result.is_a?(Mongo::Operation::GetMore::Result)
    # CursorNotFound exceptions are always resumable because the server
    # is not aware of the cursor id, and thus cannot determine if
    # the cursor is a change stream and cannot add the
    # ResumableChangeStreamError label.
    return true if code == 43

    # Connection description is not populated for unacknowledged writes.
    if connection_description.max_wire_version >= 9
      label?('ResumableChangeStreamError')
    else
      change_stream_resumable_code?
    end
  else
    false
  end
end

#connection_descriptionServer::Description

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Server description of the server that the operation that this exception refers to was performed on.

Returns:

  • (Server::Description)

    Server description of the server that the operation that this exception refers to was performed on.



38
# File 'lib/mongo/error/operation_failure.rb', line 38

def_delegator :@result, :connection_description

#initialize(message = nil, result = nil, options = {}) ⇒ Object

Create the operation failure.

Parameters:

  • message (String) (defaults to: nil)

    The error message.

  • result (Operation::Result) (defaults to: nil)

    The result object.

  • options (Hash) (defaults to: {})

    Additional parameters.

Options Hash (options):

  • :code (Integer)

    Error code.

  • :code_name (String)

    Error code name.

  • :document (BSON::Document)

    The server-returned error document.

  • server_message (String)

    The server-returned error message parsed from the response.

  • :server_address (nil | String | Mongo::Address | Mongo::Server::Description)

    The address of the server that produced the error.

  • :write_concern_error_document (Hash)

    The server-supplied write concern error document, if any.

  • :write_concern_error_code (Integer)

    Error code for write concern error, if any.

  • :write_concern_error_code_name (String)

    Error code name for write concern error, if any.

  • :write_concern_error_labels (Array<String>)

    Error labels for the write concern error, if any.

  • :labels (Array<String>)

    The set of labels associated with the error.

  • :wtimeout (true | false)

    Whether the error is a wtimeout.

Since:

  • 2.0.0



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/mongo/error/operation_failure.rb', line 192

def initialize(message = nil, result = nil, options = {})
  @details = retrieve_details(options[:document])
  @server_address = normalize_server_address(options[:server_address])
  super(append_server_address(append_details(message, @details)))

  @result = result
  @code = options[:code]
  @code_name = options[:code_name]
  @write_concern_error_document = options[:write_concern_error_document]
  @write_concern_error_code = options[:write_concern_error_code]
  @write_concern_error_code_name = options[:write_concern_error_code_name]
  @write_concern_error_labels = options[:write_concern_error_labels] || []
  @labels = options[:labels] || []
  @wtimeout = !!options[:wtimeout]
  @document = options[:document]
  @server_message = options[:server_message]
end

#max_time_ms_expired?true | false

Whether the error is MaxTimeMSExpired.

Returns:

  • (true | false)

    Whether the error is MaxTimeMSExpired.

Since:

  • 2.10.0



224
225
226
# File 'lib/mongo/error/operation_failure.rb', line 224

def max_time_ms_expired?
  code == 50 # MaxTimeMSExpired
end

#write_concern_error?true | false

Returns Whether the failure includes a write concern error. A failure may have a top level error and a write concern error or either one of the two.

Returns:

  • (true | false)

    Whether the failure includes a write concern error. A failure may have a top level error and a write concern error or either one of the two.

Since:

  • 2.10.0



129
130
131
# File 'lib/mongo/error/operation_failure.rb', line 129

def write_concern_error?
  !!@write_concern_error_document
end

#wtimeout?true | false

Whether the error is a write concern timeout.

Returns:

  • (true | false)

    Whether the error is a write concern timeout.

Since:

  • 2.7.1



215
216
217
# File 'lib/mongo/error/operation_failure.rb', line 215

def wtimeout?
  @wtimeout
end