Exception: Mongo::Error::OperationFailure

Inherits:
Mongo::Error
  • Object
show all
Extended by:
Forwardable
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 => 'InterruptedDueToReplStateChange', :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 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 inherited from Mongo::Error

#label?

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

Since:

  • 2.5.0, options added in 2.6.0



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

def initialize(message = nil, result = nil, options = {})
  @result = result
  @code = options[:code]
  @code_name = options[:code_name]
  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



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

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



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

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



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

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



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

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



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

def write_retryable?
  WRITE_RETRY_MESSAGES.any? { |m| message.include?(m) } ||
  write_retryable_code?
end