Class: RightScale::CloudApi::Parser::AWS::ResponseErrorV1

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud/aws/base/parsers/response_error.rb

Overview

AWS response error parser, case 1

Class Method Summary collapse

Class Method Details

.parse(response, options = {}) ⇒ String

Parse HTTP error message from a response body

Examples:

# For the call below:
ec2.DescribeInstances('InstanceId' => 'hohoho')
# Generates an XML error:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Errors>
    <Error>
      <Code>InvalidInstanceID.Malformed</Code>
      <Message>Invalid id: "hohoho"</Message>
    </Error>
  </Errors>
  <RequestID>84bb3005-0b9f-4a1f-9f78-8fbc2a41a401</RequestID>
</Response>
# And the method parse all above into:
400: InvalidInstanceID.Malformed: Invalid id: "hohoho" (RequestID: 84bb3005-0b9f-4a1f-9f78-8fbc2a41a401)

Parameters:

  • response (RightScale::CloudApi::HTTPResponse)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :xml_parser (Class)

Returns:

  • (String)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cloud/aws/base/parsers/response_error.rb', line 60

def self.parse(response, options={})
  result = "#{response.code}: "
  body   = response.body.to_s
  if response['content-type'].to_s[/xml/] || body[/\A<\?xml /]
    hash = Utils::get_xml_parser_class(options[:xml_parser]).parse(body)
    if hash["Response"] && hash["Response"]["Errors"]
      errors = hash["Response"]["Errors"]["Error"]
      errors = [ errors ] if errors.is_a?(Hash)
      result += errors.map{ |error| "#{error['Code']}: #{error['Message']}" }.join('; ')
      # Display a RequestId here
      result << " (RequestID: #{hash["Response"]["RequestID"]})"
    end
  else
    result << "#{body}" unless body._blank?
  end
  result
end