Class: Mongo::Error::Parser Private
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
SdamErrorDetection
|
|
Inherits: | Object |
Defined in: | lib/mongo/error/parser.rb |
Overview
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.
Constant Summary
Class Method Summary
- .build_message(code: nil, code_name: nil, message: nil) Internal use only
-
.new(document, replies = nil, options = nil) ⇒ Parser
constructor
Internal use only
Create the new parser with the returned document.
Instance Attribute Summary
- #code ⇒ Integer readonly Internal use only
- #code_name ⇒ String readonly Internal use only
- #document ⇒ BSON::Document readonly Internal use only
- #labels ⇒ Array<String> readonly Internal use only
- #message ⇒ String readonly Internal use only
- #replies ⇒ Array<Protocol::Message> readonly Internal use only
- #server_message ⇒ String readonly Internal use only
- #write_concern_error? ⇒ true | false readonly
- #wtimeout readonly Internal use only
SdamErrorDetection
- Included
#node_recovering? | Whether the error is a “node is recovering” error, or one of its variants. |
#node_shutting_down? | Whether the error is a “node is shutting down” type error. |
#not_master? | Whether the error is a “not master” error, or one of its variants. |
Instance Method Summary
- #write_concern_error_code ⇒ Integer | nil
- #write_concern_error_code_name ⇒ String | nil
-
#write_concern_error_document ⇒ Hash | nil
Returns the write concern error document as it was reported by the server, if any.
-
#write_concern_error_labels ⇒ Array<String> | nil
Internal use only
write concern error, if there is a write concern error present.
- #append(message, error) private Internal use only
- #parse! private Internal use only
- #parse_code private Internal use only
- #parse_flag(message) private Internal use only
- #parse_labels private Internal use only
- #parse_multiple(message, key) private Internal use only
- #parse_single(message, key, doc = document) private Internal use only
- #parse_wtimeout private Internal use only
Class Method Details
.build_message(code: nil, code_name: nil, message: nil)
# File 'lib/mongo/error/parser.rb', line 177
def (code: nil, code_name: nil, message: nil) if code_name && code "[#{code}:#{code_name}]: #{}" 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}]: #{}" elsif code "[#{code}]: #{}" else end end
Instance Attribute Details
#code ⇒ Integer
(readonly)
# File 'lib/mongo/error/parser.rb', line 88
attr_reader :code
#code_name ⇒ String
(readonly)
# File 'lib/mongo/error/parser.rb', line 92
attr_reader :code_name
#document ⇒ BSON::Document
(readonly)
# File 'lib/mongo/error/parser.rb', line 73
attr_reader :document
#labels ⇒ Array
<String
> (readonly)
# File 'lib/mongo/error/parser.rb', line 96
attr_reader :labels
#message ⇒ String
(readonly)
# File 'lib/mongo/error/parser.rb', line 77
attr_reader :
#replies ⇒ Array
<Protocol::Message> (readonly)
# File 'lib/mongo/error/parser.rb', line 84
attr_reader :replies
#server_message ⇒ String
(readonly)
# File 'lib/mongo/error/parser.rb', line 81
attr_reader :
#write_concern_error? ⇒ true
| false
(readonly)
# File 'lib/mongo/error/parser.rb', line 139
def write_concern_error? !!write_concern_error_document end
#wtimeout (readonly)
# File 'lib/mongo/error/parser.rb', line 99
attr_reader :wtimeout
Instance Method Details
#append(message, error) (private)
# File 'lib/mongo/error/parser.rb', line 242
def append(, error) if .length > 1 .concat(", #{error}") else .concat(error) end end
#parse! (private)
# File 'lib/mongo/error/parser.rb', line 195
def parse! if document['ok'] != 1 && document['writeErrors'] raise ArgumentError, "writeErrors should only be given in successful responses" end @message = +"" parse_single(@message, '$err') parse_single(@message, 'err') parse_single(@message, 'errmsg') parse_multiple(@message, 'writeErrors') if write_concern_error_document parse_single(@message, 'errmsg', write_concern_error_document) end parse_flag(@message) parse_code parse_labels parse_wtimeout @server_message = @message @message = self.class. ( code: code, code_name: code_name, message: @message, ) end
#parse_code (private)
# File 'lib/mongo/error/parser.rb', line 250
def parse_code if document['ok'] == 1 || @options[:legacy] @code = @code_name = nil else @code = document['code'] @code_name = document['codeName'] end # Since there is only room for one code, do not replace # codes of the top level response with write concern error codes. # In practice this should never be an issue as a write concern # can only fail after the operation succeeds on the primary. if @code.nil? && @code_name.nil? if subdoc = write_concern_error_document @code = subdoc['code'] @code_name = subdoc['codeName'] end end if @code.nil? && @code_name.nil? # If we have writeErrors, and all of their codes are the same, # use that code. Otherwise don't set the code if write_errors = document['writeErrors'] codes = write_errors.map { |e| e['code'] }.compact if codes.uniq.length == 1 @code = codes.first # code name may not be returned by the server @code_name = write_errors.map { |e| e['codeName'] }.compact.first end end end end
#parse_flag(message) (private)
#parse_labels (private)
# File 'lib/mongo/error/parser.rb', line 283
def parse_labels @labels = document['errorLabels'] || [] end
#parse_multiple(message, key) (private)
# File 'lib/mongo/error/parser.rb', line 227
def parse_multiple(, key) if errors = document[key] errors.each do |error| parse_single(, 'errmsg', error) end end end
#parse_single(message, key, doc = document) (private)
#parse_wtimeout (private)
# File 'lib/mongo/error/parser.rb', line 287
def parse_wtimeout @wtimeout = write_concern_error_document && write_concern_error_document['errInfo'] && write_concern_error_document['errInfo']['wtimeout'] end
#write_concern_error_code ⇒ Integer
| nil
# 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_name ⇒ String
| nil
# 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_document ⇒ Hash
| nil
Returns the write concern error document as it was reported by the server, if any.
# File 'lib/mongo/error/parser.rb', line 148
def write_concern_error_document document['writeConcernError'] end
#write_concern_error_labels ⇒ Array
<String
> | nil
write concern error, if there is a write concern error present.
# File 'lib/mongo/error/parser.rb', line 172
def write_concern_error_labels write_concern_error_document && write_concern_error_document['errorLabels'] end