class Mongo::Error::Parser
Class for parsing the various forms that errors can come in from MongoDB command responses.
@since 2.0.0
Attributes
code[R]
@return [ Integer ] code The error code parsed from the document. @since 2.6.0
code_name[R]
@return [ String ] #code_name The error code name parsed from the document. @since 2.6.0
document[R]
@return [ BSON::Document ] document The returned document.
labels[R]
@return [ Array<String> ] labels The set of labels associated with the error. @since 2.7.0
message[R]
@return [ String ] message The error message parsed from the document.
replies[R]
@return [ Array<Protocol::Message> ] replies The message replies.
wtimeout[R]
@api private
Public Class Methods
new(document, replies = nil)
click to toggle source
Create the new parser with the returned document.
@example Create the new parser.
Parser.new({ 'errmsg' => 'failed' })
@param [ BSON::Document ] document The returned document.
@since 2.0.0
# File lib/mongo/error/parser.rb, line 81 def initialize(document, replies = nil) @document = document || {} @replies = replies parse! end
Private Instance Methods
append(message, error)
click to toggle source
# File lib/mongo/error/parser.rb, line 124 def append(message, error) if message.length > 1 message.concat(", #{error}") else message.concat(error) end end
parse!()
click to toggle source
# File lib/mongo/error/parser.rb, line 89 def parse! @message = "" parse_single(@message, ERR) parse_single(@message, ERROR) parse_single(@message, ERRMSG) parse_multiple(@message, WRITE_ERRORS) parse_single(@message, ERRMSG, document[WRITE_CONCERN_ERROR]) if document[WRITE_CONCERN_ERROR] parse_flag(@message) parse_code parse_labels parse_wtimeout end
parse_code()
click to toggle source
# File lib/mongo/error/parser.rb, line 132 def parse_code @code = document['code'] @code_name = document['codeName'] # 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 = document[WRITE_CONCERN_ERROR] @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[WRITE_ERRORS] 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)
click to toggle source
# File lib/mongo/error/parser.rb, line 117 def parse_flag(message) if replies && replies.first && (replies.first.respond_to?(:cursor_not_found?)) && replies.first.cursor_not_found? append(message, CURSOR_NOT_FOUND) end end
parse_labels()
click to toggle source
# File lib/mongo/error/parser.rb, line 161 def parse_labels @labels = document['errorLabels'] || [] end
parse_multiple(message, key)
click to toggle source
# File lib/mongo/error/parser.rb, line 109 def parse_multiple(message, key) if errors = document[key] errors.each do |error| parse_single(message, ERRMSG, error) end end end
parse_single(message, key, doc = document)
click to toggle source
# File lib/mongo/error/parser.rb, line 103 def parse_single(message, key, doc = document) if error = doc[key] append(message ,"#{error} (#{doc[CODE]})") end end
parse_wtimeout()
click to toggle source
# File lib/mongo/error/parser.rb, line 165 def parse_wtimeout @wtimeout = document[WRITE_CONCERN_ERROR] && document[WRITE_CONCERN_ERROR]['errInfo'] && document[WRITE_CONCERN_ERROR]['errInfo']['wtimeout'] end