Exception: Mongo::Error::OperationFailure

Inherits:
Mongo::Error
  • Object
show all
Extended by:
Forwardable
Includes:
SdamErrorDetection
Defined in:
lib/mongo/error/operation_failure.rb

Overview

Raised when an operation fails for some reason.

Since:

  • 2.0.0

Constant Summary collapse

WRITE_RETRY_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 write being retried.

Since:

  • 2.6.0

[
  {:code_name => 'InterruptedAtShutdown', :code => 11600},
  {:code_name => 'InterruptedDueToStepDown', :code => 11602},
  {:code_name => 'NotMaster', :code => 10107},
  {:code_name => 'NotMasterNoSlaveOk', :code => 13435},
  {:code_name => 'NotMasterOrSecondary', :code => 13436},
  {:code_name => 'PrimarySteppedDown', :code => 189},
  {:code_name => 'ShutdownInProgress', :code => 91},
  {:code_name => 'HostNotFound', :code => 7},
  {:code_name => 'HostUnreachable', :code => 6},
  {:code_name => 'NetworkTimeout', :code => 89},
  {:code_name => 'SocketException', :code => 9001},
].freeze
WRITE_RETRY_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.

These are magic error messages that could indicate a master change.

Since:

  • 2.4.2

[
  'not master',
  'node is recovering',
].freeze
RETRY_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.

These are magic error messages that could indicate a cluster reconfiguration behind a mongos.

Since:

  • 2.1.1

WRITE_RETRY_MESSAGES + [
  'transport error',
  'socket exception',
  "can't connect",
  'connect failed',
  'error querying',
  'could not get last error',
  'connection attempt failed',
  'interrupted at shutdown',
  'unknown replica set',
  'dbclient error communicating with server'
].freeze
CHANGE_STREAM_NOT_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.6.0

[
  {:code_name => 'CappedPositionLost', :code => 136},
  {:code_name => 'CursorKilled', :code => 237},
  {:code_name => 'Interrupted', :code => 11601},
].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

WRITE_RETRY_MESSAGES

Constants included from SdamErrorDetection

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

Constants inherited from Mongo::Error

BAD_VALUE, CODE, CURSOR_NOT_FOUND, ERR, ERRMSG, ERROR, TRANSIENT_TRANSACTION_ERROR_LABEL, UNKNOWN_ERROR, UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL, WRITE_CONCERN_ERROR, WRITE_CONCERN_ERRORS, WRITE_ERRORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SdamErrorDetection

#node_recovering?, #node_shutting_down?, #not_master?

Methods inherited from Mongo::Error

#add_label, #label?, #labels

Methods included from Notable

#add_note, #inspect, #message, #notes, #to_s

Constructor Details

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

Create the operation failure.

Examples:

Create the error object

OperationFailure.new(message, result)

Create the error object with a code and a code name

OperationFailure.new(message, result, :code => code, :code_name => code_name)

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.

  • :write_concern_error (true | false)

    Whether the write concern error is present.

  • :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.

  • :labels (Array<String>)

    The set of labels associated with the error.

  • :wtimeout (true | false)

    Whether the error is a wtimeout.

Since:

  • 2.5.0, options added in 2.6.0



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/mongo/error/operation_failure.rb', line 226

def initialize(message = nil, result = nil, options = {})
  super(message)
  @result = result
  @code = options[:code]
  @code_name = options[:code_name]
  @write_concern_error = !!options[:write_concern_error]
  @write_concern_error_code = options[:write_concern_error_code]
  @write_concern_error_code_name = options[:write_concern_error_code_name]
  @labels = options[:labels] || []
  @wtimeout = !!options[:wtimeout]
end

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



76
77
78
# File 'lib/mongo/error/operation_failure.rb', line 76

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



81
82
83
# File 'lib/mongo/error/operation_failure.rb', line 81

def code_name
  @code_name
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



192
193
194
# File 'lib/mongo/error/operation_failure.rb', line 192

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



199
200
201
# File 'lib/mongo/error/operation_failure.rb', line 199

def write_concern_error_code_name
  @write_concern_error_code_name
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



144
145
146
147
148
149
150
151
152
# File 'lib/mongo/error/operation_failure.rb', line 144

def change_stream_resumable?
  if @result && @result.is_a?(Mongo::Operation::GetMore::Result)
    !change_stream_not_resumable_label? &&
    (change_stream_resumable_message? ||
    change_stream_resumable_code?)
  else
    false
  end
end

#max_time_ms_expired?true | false

Whether the error is MaxTimeMSExpired.

Returns:

  • (true | false)

    Whether the error is MaxTimeMSExpired.

Since:

  • 2.10.0



252
253
254
# File 'lib/mongo/error/operation_failure.rb', line 252

def max_time_ms_expired?
  code == 50 # MaxTimeMSExpired
end

#retryable?true, false

Deprecated.

Whether the error is a retryable error according to the legacy read retry logic.

Returns:

  • (true, false)

Since:

  • 2.1.1



90
91
92
# File 'lib/mongo/error/operation_failure.rb', line 90

def retryable?
  RETRY_MESSAGES.any?{ |m| message.include?(m) }
end

#unsupported_retryable_write?true | false

Whether the error is caused by an attempted retryable write on a storage engine that does not support retryable writes.

retryable write on a storage engine that does not support retryable writes.

Returns:

  • (true | false)

    Whether the error is caused by an attempted

Since:

  • 2.10.0



263
264
265
266
# File 'lib/mongo/error/operation_failure.rb', line 263

def unsupported_retryable_write?
  # code 20 is IllegalOperation
  code == 20 && message.start_with?("Transaction numbers")
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



183
184
185
# File 'lib/mongo/error/operation_failure.rb', line 183

def write_concern_error?
  @write_concern_error
end

#write_retryable?true, false

Whether the error is a retryable error according to the modern retryable reads and retryable writes specifications.

This method is also used by the legacy retryable write logic to determine whether an error is a retryable one.

Returns:

  • (true, false)

Since:

  • 2.4.2



103
104
105
106
# File 'lib/mongo/error/operation_failure.rb', line 103

def write_retryable?
  WRITE_RETRY_MESSAGES.any? { |m| message.include?(m) } ||
  write_retryable_code?
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



243
244
245
# File 'lib/mongo/error/operation_failure.rb', line 243

def wtimeout?
  @wtimeout
end