Class: Mongo::Error::Parser

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

Overview

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

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

Instance Method Summary collapse

Methods included from SdamErrorDetection

#node_recovering?, #node_shutting_down?, #not_master?

Constructor Details

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

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



94
95
96
97
98
99
100
101
102
103
# File 'lib/mongo/error/parser.rb', line 94

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)

Returns code The error code parsed from the document.

Returns:

  • (Integer)

    code The error code parsed from the document.

Since:

  • 2.6.0



60
61
62
# File 'lib/mongo/error/parser.rb', line 60

def code
  @code
end

#code_nameString (readonly)

Returns code_name The error code name parsed from the document.

Returns:

  • (String)

    code_name The error code name parsed from the document.

Since:

  • 2.6.0



64
65
66
# File 'lib/mongo/error/parser.rb', line 64

def code_name
  @code_name
end

#documentBSON::Document (readonly)

Returns document The returned document.

Returns:

  • (BSON::Document)

    document The returned document.

Since:

  • 2.0.0



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

def document
  @document
end

#labelsArray<String> (readonly)

Returns labels The set of labels associated with the error.

Returns:

  • (Array<String>)

    labels The set of labels associated with the error.

Since:

  • 2.7.0



68
69
70
# File 'lib/mongo/error/parser.rb', line 68

def labels
  @labels
end

#messageString (readonly)

Returns message The error message parsed from the document.

Returns:

  • (String)

    message The error message parsed from the document.

Since:

  • 2.0.0



53
54
55
# File 'lib/mongo/error/parser.rb', line 53

def message
  @message
end

#repliesArray<Protocol::Message> (readonly)

Returns replies The message replies.

Returns:

Since:

  • 2.0.0



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

def replies
  @replies
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



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

def wtimeout
  @wtimeout
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



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

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