Class: RightScale::CloudApi::Parser::ECS::ResponseError

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud/ecs/pa/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"?>
  <ItemLookupErrorResponse xmlns="http://ecs.amazonaws.com/doc/2013-08-01/">
    <Error>
      <Code>
        AWS.InvalidAccount
      </Code>
      <Message>
        Your AccessKey Id is not registered for Product Advertising API. Please use the AccessKey Id obtained after registering at https://affiliate-program.amazon.com/gp/flex/advertising/api/sign-in.html
      </Message>
    </Error>
    <RequestId>
      57510d4f-72ad-4c15-985c-1dc330e5f3d4
    </RequestId>
  </ItemLookupErrorResponse>
# And the method parse all above into:
400: Your AccessKey Id is not registered for Product Advertising API. Please use the AccessKey Id obtained after registering at https://affiliate-program.amazon.com/gp/flex/advertising/api/sign-in.html (RequestID: 57510d4f-72ad-4c15-985c-1dc330e5f3d4)

Parameters:

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

Options Hash (options):

  • :xml_parser (Class)

Returns:

  • (String)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cloud/ecs/pa/parsers/response_error.rb', line 64

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)
    top_key = hash.keys.first
    error   = hash[top_key] && hash[top_key]["Error"]
    if error
      result << ('%s: %s' % [error['Code'], error['Message']])
      result << (' (RequestID: %s)' % hash[top_key]['RequestId'])
    end
  else
    result << body unless body._blank?
  end
  result
end