Class: Mongo::Error::Parser Private

Inherits:
Object
  • Object
show all
Includes:
SdamErrorDetection
Defined in:
lib/mongo/error/parser.rb

Overview

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

Class for parsing the various forms that errors can come in from MongoDB command responses.

The errors can be reported by the server in a number of ways:

  • ok:0 response indicates failure. In newer servers, code, codeName and errmsg fields should be set. In older servers some may not be set.

  • ok:1 response with a write concern error (writeConcernError top-level field). This indicates that the node responding successfully executed the request, but not enough other nodes successfully executed the request to satisfy the write concern.

  • ok:1 response with writeErrors top-level field. This can be obtained in a bulk write but also in a non-bulk write. In a non-bulk write there should be exactly one error in the writeErrors list. The case of multiple errors is handled by BulkWrite::Result.

  • ok:1 response with writeConcernErrors top-level field. This can only be obtained in a bulk write and is handled by BulkWrite::Result, not by this class.

Note that writeErrors do not have codeName fields - they just provide codes and messages. writeConcernErrors may similarly not provide code names.

Since:

  • 2.0.0

Constant Summary

Constants included from SdamErrorDetection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SdamErrorDetection

#node_recovering?, #node_shutting_down?, #not_master?

Constructor Details

#initialize(document, replies = nil, options = nil) ⇒ Parser

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.

Create the new parser with the returned document.

In legacy mode, the code and codeName fields of the document are not examined because the status (ok: 1) is not part of the document and there is no way to distinguish successful from failed responses using the document itself, and a successful response may legitimately have { code: 123, codeName: ‘foo’ } as the contents of a user-inserted document. The legacy server versions do not fill out code nor codeName thus not reading them does not lose information.

Examples:

Create the new parser.

Parser.new({ 'errmsg' => 'failed' })

Parameters:

  • document (BSON::Document)

    The returned document.

  • replies (Array<Protocol::Message>) (defaults to: nil)

    The message replies.

  • options (Hash) (defaults to: nil)

    The options.

Options Hash (options):

  • :legacy (true | false)

    Whether document and replies are from a legacy (pre-3.2) response

Since:

  • 2.0.0



122
123
124
125
126
127
128
129
130
131
# File 'lib/mongo/error/parser.rb', line 122

def initialize(document, replies = nil, options = nil)
  @document = document || {}
  @replies = replies
  @options = if options
    options.dup
  else
    {}
  end.freeze
  parse!
end

Instance Attribute Details

#codeInteger (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 error code parsed from the document.

Returns:

  • (Integer)

    The error code parsed from the document.

Since:

  • 2.6.0



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

def code
  @code
end

#code_nameString (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 error code name parsed from the document.

Returns:

  • (String)

    The error code name parsed from the document.

Since:

  • 2.6.0



92
93
94
# File 'lib/mongo/error/parser.rb', line 92

def code_name
  @code_name
end

#documentBSON::Document (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 returned document.

Returns:

  • (BSON::Document)

    The returned document.

Since:

  • 2.0.0



73
74
75
# File 'lib/mongo/error/parser.rb', line 73

def document
  @document
end

#labelsArray<String> (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 set of labels associated with the error.

Returns:

  • (Array<String>)

    The set of labels associated with the error.

Since:

  • 2.7.0



96
97
98
# File 'lib/mongo/error/parser.rb', line 96

def labels
  @labels
end

#messageString (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 full error message to be used in the raised exception.

Returns:

  • (String)

    The full error message to be used in the raised exception.

Since:

  • 2.0.0



77
78
79
# File 'lib/mongo/error/parser.rb', line 77

def message
  @message
end

#repliesArray<Protocol::Message> (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 message replies.

Returns:

Since:

  • 2.0.0



84
85
86
# File 'lib/mongo/error/parser.rb', line 84

def replies
  @replies
end

#server_messageString (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 server-returned error message parsed from the response.

Returns:

  • (String)

    The server-returned error message parsed from the response.

Since:

  • 2.0.0



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

def server_message
  @server_message
end

#wtimeoutObject (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.

Since:

  • 2.0.0



99
100
101
# File 'lib/mongo/error/parser.rb', line 99

def wtimeout
  @wtimeout
end

Class Method Details

.build_message(code: nil, code_name: nil, message: nil) ⇒ Object

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.

Since:

  • 2.0.0



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mongo/error/parser.rb', line 177

def build_message(code: nil, code_name: nil, message: nil)
  if code_name && code
    "[#{code}:#{code_name}]: #{message}"
  elsif code_name
    # This surely should never happen, if there's a code name
    # there ought to also be the code provided.
    # Handle this case for completeness.
    "[#{code_name}]: #{message}"
  elsif code
    "[#{code}]: #{message}"
  else
    message
  end
end

Instance Method Details

#write_concern_error?true | false

Returns Whether the document 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 document 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



139
140
141
# File 'lib/mongo/error/parser.rb', line 139

def write_concern_error?
  !!write_concern_error_document
end

#write_concern_error_codeInteger | nil

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



157
158
159
# File 'lib/mongo/error/parser.rb', line 157

def write_concern_error_code
  write_concern_error_document && write_concern_error_document['code']
end

#write_concern_error_code_nameString | nil

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



166
167
168
# File 'lib/mongo/error/parser.rb', line 166

def write_concern_error_code_name
  write_concern_error_document && write_concern_error_document['codeName']
end

#write_concern_error_documentHash | nil

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



148
149
150
# File 'lib/mongo/error/parser.rb', line 148

def write_concern_error_document
  document['writeConcernError']
end

#write_concern_error_labelsArray<String> | nil

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.

write concern error, if there is a write concern error present.

Returns:

  • (Array<String> | nil)

    The error labels associated with this

Since:

  • 2.0.0



172
173
174
# File 'lib/mongo/error/parser.rb', line 172

def write_concern_error_labels
  write_concern_error_document && write_concern_error_document['errorLabels']
end