class Yardi::Validator::ErrorResponse
Ensure that the response has no Messages with messageType=Error.
Attributes
Public Class Methods
@param parsed_response [Hash<String, Object>] XML response parsed into a
Hash
# File lib/yardi/validator/error_response.rb, line 9 def initialize(action:, parsed_response:) @action = action @response = parsed_response end
Public Instance Methods
@raise [Yardi::Error::ErrorResponse] if the response has an error
# File lib/yardi/validator/error_response.rb, line 15 def validate! return unless error? guest_err_msg = "Error: No guests exist with the given search criteria." if error_messages.include?(guest_err_msg) raise Error::GuestsNotFound end raise Error::ErrorResponse, error_messages.join('. ') end
Private Instance Methods
# File lib/yardi/validator/error_response.rb, line 30 def error? messages.any? do |message_node| message_node['messageType'].downcase == 'error' end end
We have seen a few types of error messages from Yardi
so far.
1) there is only one Message node. 2) error response contains two message nodes that have the same content. (this is why we need the .uniq!) 3) error response contains two (or more) nodes with different content
PRECONDITION: error?
evaluates to true.
# File lib/yardi/validator/error_response.rb, line 80 def error_messages messages.map do |message| "#{message['messageType']}: #{message['__content__']}".strip end.uniq end
We have seen a few different types of errors from Yardi
. The content inside <Messages><Message> can be a hash, an array of hashes, or an array that has both a string and a hash. This string case is the example in missing_node_error.xml; they have two <Message> nodes, both of which have the same content, but only one of which has the 'messageType' attribute to tell us this is an error case. When XML is parsed into a hash, this becomes: [
"XSD Error:The element 'Name' has incomplete...", { "messageType" => "Error", "__content__" => "XSD Error:The element 'Name' has incomplete..." }
] We convert the string into a hash that matches the rest of the error messages so that we don't have to worry about type checks when pulling out the errors.
# File lib/yardi/validator/error_response.rb, line 53 def messages body = response['soap:Envelope']['soap:Body'] # This will be picked up by either the EmptyResponse or the # FaultResponse check. Whatever it is, it's not an ErrorResponse issue. return [] if body["#{action}Response"].nil? result = body["#{action}Response"]["#{action}Result"] # There are no Messages nodes in a successful # GetYardiGuestActivity_Search response return [] if result['Messages'].nil? nodes = result['Messages']['Message'] nodes = nodes.is_a?(Array) ? nodes : [nodes] # see method comment for why this normalization is needed nodes.map do |node| if node.is_a?(Hash) node elsif node.is_a?(String) { 'messageType' => 'Error', '__content__' => node } end end end