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.

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.

Since:

  • 2.0.0



85
86
87
88
89
90
91
92
93
94
# File 'lib/mongo/error/parser.rb', line 85

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



62
63
64
# File 'lib/mongo/error/parser.rb', line 62

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



66
67
68
# File 'lib/mongo/error/parser.rb', line 66

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



47
48
49
# File 'lib/mongo/error/parser.rb', line 47

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



70
71
72
# File 'lib/mongo/error/parser.rb', line 70

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



51
52
53
# File 'lib/mongo/error/parser.rb', line 51

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



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

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



55
56
57
# File 'lib/mongo/error/parser.rb', line 55

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



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

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/mongo/error/parser.rb', line 140

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



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

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



120
121
122
# File 'lib/mongo/error/parser.rb', line 120

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



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

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



111
112
113
# File 'lib/mongo/error/parser.rb', line 111

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



135
136
137
# File 'lib/mongo/error/parser.rb', line 135

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