class Mongo::Error::Parser

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

@since 2.0.0

Attributes

document[R]

@return [ BSON::Document ] document The returned document.

message[R]

@return [ String ] message The error message parsed from the document.

replies[R]

@return [ Array<Protocol::Reply> ] replies The message replies.

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 41
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 80
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 49
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)
end
parse_flag(message) click to toggle source
# File lib/mongo/error/parser.rb, line 74
def parse_flag(message)
  if replies && replies.first && replies.first.cursor_not_found?
    append(message, CURSOR_NOT_FOUND)
  end
end
parse_multiple(message, key) click to toggle source
# File lib/mongo/error/parser.rb, line 66
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 60
def parse_single(message, key, doc = document)
  if error = doc[key]
    append(message ,"#{error} (#{doc[CODE]})")
  end
end