# File lib/AWS/Autoscaling.rb, line 29 29: def aws_error?(response) 30: 31: # return false if we got a HTTP 200 code, 32: # otherwise there is some type of error (40x,50x) and 33: # we should try to raise an appropriate exception 34: # from one of our exception classes defined in 35: # exceptions.rb 36: return false if response.is_a?(Net::HTTPSuccess) 37: 38: # parse the XML document so we can walk through it 39: doc = REXML::Document.new(response.body) 40: 41: # Check that the Error element is in the place we would expect. 42: # and if not raise a generic error exception 43: unless doc.root.elements[1].name == "Error" 44: raise Error, "Unexpected error format. response.body is: #{response.body}" 45: end 46: 47: # An valid error response looks like this: 48: # <?xml version="1.0"?><Response><Errors><Error><Code>InvalidParameterCombination</Code><Message>Unknown parameter: foo</Message></Error></Errors><RequestID>291cef62-3e86-414b-900e-17246eccfae8</RequestID></Response> 49: # AWS EC2 throws some exception codes that look like Error.SubError. Since we can't name classes this way 50: # we need to strip out the '.' in the error 'Code' and we name the error exceptions with this 51: # non '.' name as well. 52: error_code = doc.root.elements['//ErrorResponse/Error/Code'].text.gsub('.', '') 53: error_message = doc.root.elements['//ErrorResponse/Error/Message'].text 54: 55: # Raise one of our specific error classes if it exists. 56: # otherwise, throw a generic EC2 Error with a few details. 57: if AWS.const_defined?(error_code) 58: raise AWS.const_get(error_code), error_message 59: else 60: raise AWS::Error, error_message 61: end 62: 63: end