# File lib/AWS.rb, line 332 332: def aws_error?(response) 333: 334: # return false if we got a HTTP 200 code, 335: # otherwise there is some type of error (40x,50x) and 336: # we should try to raise an appropriate exception 337: # from one of our exception classes defined in 338: # exceptions.rb 339: return false if response.is_a?(Net::HTTPSuccess) 340: 341: raise AWS::Error, "Unexpected server error. response.body is: #{response.body}" if response.is_a?(Net::HTTPServerError) 342: 343: # parse the XML document so we can walk through it 344: doc = REXML::Document.new(response.body) 345: 346: # Check that the Error element is in the place we would expect. 347: # and if not raise a generic error exception 348: unless doc.root.elements['Errors'].elements['Error'].name == 'Error' 349: raise Error, "Unexpected error format. response.body is: #{response.body}" 350: end 351: 352: # An valid error response looks like this: 353: # <?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> 354: # AWS throws some exception codes that look like Error.SubError. Since we can't name classes this way 355: # we need to strip out the '.' in the error 'Code' and we name the error exceptions with this 356: # non '.' name as well. 357: error_code = doc.root.elements['Errors'].elements['Error'].elements['Code'].text.gsub('.', '') 358: error_message = doc.root.elements['Errors'].elements['Error'].elements['Message'].text 359: 360: # Raise one of our specific error classes if it exists. 361: # otherwise, throw a generic EC2 Error with a few details. 362: if AWS.const_defined?(error_code) 363: raise AWS.const_get(error_code), error_message 364: else 365: raise AWS::Error, error_message 366: end 367: 368: end