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::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?, #not_master?

Methods inherited from Mongo::Error

#label?, #labels

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

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



184
185
186
187
188
189
190
191
# File 'lib/mongo/error/operation_failure.rb', line 184

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

Instance Attribute Details

#codeInteger (readonly)

Returns code The error code parsed from the document.

Returns:

  • (Integer)

    code The error code parsed from the document.

Since:

  • 2.6.0



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

def code
  @code
end

#code_nameString (readonly)

Returns code_name The error code name parsed from the document.

Returns:

  • (String)

    code_name The error code name parsed from the document.

Since:

  • 2.6.0



79
80
81
# File 'lib/mongo/error/operation_failure.rb', line 79

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



142
143
144
145
146
147
148
149
# File 'lib/mongo/error/operation_failure.rb', line 142

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

#retryable?true, false

Can the read operation that caused the error be retried?

Examples:

Is the error retryable?

error.retryable?

Returns:

  • (true, false)

    Whether the error is retryable.

Since:

  • 2.1.1



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

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

#write_retryable?true, false

Can the write operation that caused the error be retried?

Examples:

Is the error retryable for writes?

error.write_retryable?

Returns:

  • (true, false)

    Whether the error is retryable.

Since:

  • 2.4.2



101
102
103
104
# File 'lib/mongo/error/operation_failure.rb', line 101

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



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

def wtimeout?
  @wtimeout
end