1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #1935 from jbence/feature/update_AWS_error_handling

[aws] Update aws error handling
This commit is contained in:
Wesley Beary 2013-07-03 10:07:29 -07:00
commit 9cdc2bce09
10 changed files with 126 additions and 138 deletions

View file

@ -301,5 +301,16 @@ module Fog
end end
options options
end end
module Errors
def self.match_error(error)
matcher = lambda {|s| s.match(/(?:.*<Code>(.*)<\/Code>)(?:.*<Message>(.*)<\/Message>)/m)}
[error.message, error.response.body].each(&Proc.new {|s|
match = matcher.call(s)
return {:code => match[1].split('.').last, :message => match[2]} if match
})
{} # we did not match the message or response body
end
end
end end
end end

View file

@ -143,7 +143,7 @@ module Fog
def _request(body, idempotent, parser) def _request(body, idempotent, parser)
begin begin
response = @connection.request({ @connection.request({
:body => body, :body => body,
:expects => 200, :expects => 200,
:idempotent => idempotent, :idempotent => idempotent,
@ -153,24 +153,19 @@ module Fog
:parser => parser :parser => parser
}) })
rescue Excon::Errors::HTTPStatusError => error rescue Excon::Errors::HTTPStatusError => error
if match = error.message.match(/(?:.*<Code>(.*)<\/Code>)(?:.*<Message>(.*)<\/Message>)/m) match = Fog::AWS::Errors.match_error(error)
case match[1] raise if match.empty?
when 'AlreadyExists' raise case match[:code]
#raise Fog::AWS::AutoScaling::IdentifierTaken.new(match[2]) when 'AlreadyExists'
raise Fog::AWS::AutoScaling::IdentifierTaken.slurp(error, match[2]) Fog::AWS::AutoScaling::IdentifierTaken.slurp(error, match[:message])
when 'ResourceInUse' when 'ResourceInUse'
raise Fog::AWS::AutoScaling::ResourceInUse.slurp(error, match[2]) Fog::AWS::AutoScaling::ResourceInUse.slurp(error, match[:message])
when 'ValidationError' when 'ValidationError'
raise Fog::AWS::AutoScaling::ValidationError.slurp(error, match[2]) Fog::AWS::AutoScaling::ValidationError.slurp(error, match[:message])
else else
raise Fog::Compute::AWS::Error.slurp(error, "#{match[1]} => #{match[2]}") Fog::AWS::AutoScaling::Error.slurp(error, "#{match[:code]} => #{match[:message]}")
end end
else
raise
end
end end
response
end end
def setup_credentials(options) def setup_credentials(options)

View file

@ -131,16 +131,14 @@ module Fog
:parser => parser :parser => parser
}) })
rescue Excon::Errors::HTTPStatusError => error rescue Excon::Errors::HTTPStatusError => error
if match = error.message.match(/(?:.*<Code>(.*)<\/Code>)(?:.*<Message>(.*)<\/Message>)/m) match = Fog::AWS::Errors.match_error(error)
raise case match[1].split('.').last raise if match.empty?
when 'InvalidParameterValue' raise case match[:code]
Fog::AWS::ElasticBeanstalk::InvalidParameterError.slurp(error, match[2]) when 'InvalidParameterValue'
else Fog::AWS::ElasticBeanstalk::InvalidParameterError.slurp(error, match[:message])
Fog::AWS::ElasticBeanstalk::Error.slurp(error, "#{match[1]} => #{match[2]}") else
end Fog::AWS::ElasticBeanstalk::Error.slurp(error, "#{match[:code]} => #{match[:message]}")
else end
raise error
end
end end
end end

View file

@ -98,7 +98,7 @@ module Fog
) )
begin begin
response = @connection.request({ @connection.request({
:body => body, :body => body,
:expects => 200, :expects => 200,
:idempotent => idempotent, :idempotent => idempotent,
@ -108,19 +108,16 @@ module Fog
:parser => parser :parser => parser
}) })
rescue Excon::Errors::HTTPStatusError => error rescue Excon::Errors::HTTPStatusError => error
if match = error.message.match(/(?:.*<Code>(.*)<\/Code>)(?:.*<Message>(.*)<\/Message>)/m) match = Fog::AWS::Errors.match_error(error)
raise case match[1].split('.').last raise if match.empty?
when 'NotFound', 'ValidationError' raise case match[:code]
Fog::AWS::CloudFormation::NotFound.slurp(error, match[2]) when 'NotFound', 'ValidationError'
else Fog::AWS::CloudFormation::NotFound.slurp(error, match[:message])
Fog::AWS::CloudFormation::Error.slurp(error, "#{match[1]} => #{match[2]}") else
end Fog::AWS::CloudFormation::Error.slurp(error, "#{match[:code]} => #{match[:message]}")
else end
raise error
end
end end
response
end end
end end

View file

@ -392,16 +392,14 @@ module Fog
:parser => parser :parser => parser
}) })
rescue Excon::Errors::HTTPStatusError => error rescue Excon::Errors::HTTPStatusError => error
if match = error.message.match(/(?:.*<Code>(.*)<\/Code>)(?:.*<Message>(.*)<\/Message>)/m) match = Fog::AWS::Errors.match_error(error)
raise case match[1].split('.').last raise if match.empty?
when 'NotFound', 'Unknown' raise case match[:code]
Fog::Compute::AWS::NotFound.slurp(error, match[2]) when 'NotFound', 'Unknown'
else Fog::Compute::AWS::NotFound.slurp(error, match[:message])
Fog::Compute::AWS::Error.slurp(error, "#{match[1]} => #{match[2]}") else
end Fog::Compute::AWS::Error.slurp(error, "#{match[:code]} => #{match[:message]}")
else end
raise error
end
end end
end end

View file

@ -94,7 +94,7 @@ module Fog
) )
begin begin
response = @connection.request({ @connection.request({
:body => body, :body => body,
:expects => 200, :expects => 200,
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' }, :headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
@ -104,24 +104,20 @@ module Fog
:parser => parser :parser => parser
}) })
rescue Excon::Errors::HTTPStatusError => error rescue Excon::Errors::HTTPStatusError => error
if match = error.message.match(/(?:.*<Code>(.*)<\/Code>?)/m) match = Fog::AWS::Errors.match_error(error)
case match[1] raise if match.empty?
when 'CacheSecurityGroupNotFound', 'CacheParameterGroupNotFound', raise case match[:code]
'CacheClusterNotFound' when 'CacheSecurityGroupNotFound', 'CacheParameterGroupNotFound', 'CacheClusterNotFound'
raise Fog::AWS::Elasticache::NotFound Fog::AWS::Elasticache::NotFound.slurp(error, match[:message])
when 'CacheSecurityGroupAlreadyExists' when 'CacheSecurityGroupAlreadyExists'
raise Fog::AWS::Elasticache::IdentifierTaken Fog::AWS::Elasticache::IdentifierTaken.slurp(error, match[:message])
when 'InvalidParameterValue' when 'InvalidParameterValue'
raise Fog::AWS::Elasticache::InvalidInstance Fog::AWS::Elasticache::InvalidInstance.slurp(error, match[:message])
else else
raise Fog::AWS::Elasticache::Error.slurp(error, "#{match[:code]} => #{match[:message]}")
end end
else
raise
end
end end
response
end end
end end

View file

@ -191,37 +191,35 @@ module Fog
:parser => parser :parser => parser
}) })
rescue Excon::Errors::HTTPStatusError => error rescue Excon::Errors::HTTPStatusError => error
if match = error.message.match(/(?:.*<Code>(.*)<\/Code>)(?:.*<Message>(.*)<\/Message>)/m) match = Fog::AWS::Errors.match_error(error)
case match[1] raise if match.empty?
when 'CertificateNotFound' raise case match[:code]
raise Fog::AWS::IAM::NotFound.slurp(error, match[2]) when 'CertificateNotFound'
when 'DuplicateLoadBalancerName' Fog::AWS::IAM::NotFound.slurp(error, match[:message])
raise Fog::AWS::ELB::IdentifierTaken.slurp(error, match[2]) when 'DuplicateLoadBalancerName'
when 'DuplicatePolicyName' Fog::AWS::ELB::IdentifierTaken.slurp(error, match[:message])
raise Fog::AWS::ELB::DuplicatePolicyName.slurp(error, match[2]) when 'DuplicatePolicyName'
when 'InvalidInstance' Fog::AWS::ELB::DuplicatePolicyName.slurp(error, match[:message])
raise Fog::AWS::ELB::InvalidInstance.slurp(error, match[2]) when 'InvalidInstance'
when 'InvalidConfigurationRequest' Fog::AWS::ELB::InvalidInstance.slurp(error, match[:message])
# when do they fucking use this shit? when 'InvalidConfigurationRequest'
raise Fog::AWS::ELB::InvalidConfigurationRequest.slurp(error, match[2]) # when do they fucking use this shit?
when 'LoadBalancerNotFound' Fog::AWS::ELB::InvalidConfigurationRequest.slurp(error, match[:message])
raise Fog::AWS::ELB::NotFound.slurp(error, match[2]) when 'LoadBalancerNotFound'
when 'PolicyNotFound' Fog::AWS::ELB::NotFound.slurp(error, match[:message])
raise Fog::AWS::ELB::PolicyNotFound.slurp(error, match[2]) when 'PolicyNotFound'
when 'PolicyTypeNotFound' Fog::AWS::ELB::PolicyNotFound.slurp(error, match[:message])
raise Fog::AWS::ELB::PolicyTypeNotFound.slurp(error, match[2]) when 'PolicyTypeNotFound'
when 'Throttling' Fog::AWS::ELB::PolicyTypeNotFound.slurp(error, match[:message])
raise Fog::AWS::ELB::Throttled.slurp(error, match[2]) when 'Throttling'
when 'TooManyPolicies' Fog::AWS::ELB::Throttled.slurp(error, match[:message])
raise Fog::AWS::ELB::TooManyPolicies.slurp(error, match[2]) when 'TooManyPolicies'
when 'ValidationError' Fog::AWS::ELB::TooManyPolicies.slurp(error, match[:message])
raise Fog::AWS::ELB::ValidationError.slurp(error, match[2]) when 'ValidationError'
else Fog::AWS::ELB::ValidationError.slurp(error, match[:message])
raise else
end Fog::AWS::ELB::Error.slurp(error, "#{match[:code]} => #{match[:message]}")
else end
raise
end
end end
end end
end end

View file

@ -208,19 +208,16 @@ module Fog
:parser => parser :parser => parser
}) })
rescue Excon::Errors::HTTPStatusError => error rescue Excon::Errors::HTTPStatusError => error
if match = error.message.match(/(?:.*<Code>(.*)<\/Code>)(?:.*<Message>(.*)<\/Message>)/m) match = Fog::AWS::Errors.match_error(error)
case match[1] raise if match.empty?
when 'CertificateNotFound', 'NoSuchEntity' raise case match[:code]
raise Fog::AWS::IAM::NotFound.slurp(error, match[2]) when 'CertificateNotFound', 'NoSuchEntity'
when 'EntityAlreadyExists', 'KeyPairMismatch', 'LimitExceeded', 'MalformedCertificate', 'ValidationError' Fog::AWS::IAM::NotFound.slurp(error, match[:message])
raise Fog::AWS::IAM.const_get(match[1]).slurp(error, match[2]) when 'EntityAlreadyExists', 'KeyPairMismatch', 'LimitExceeded', 'MalformedCertificate', 'ValidationError'
else Fog::AWS::IAM.const_get(match[:code]).slurp(error, match[:message])
raise Fog::AWS::IAM::Error.slurp(error, "#{match[1]} => #{match[2]}") if match[1] else
raise Fog::AWS::IAM::Error.slurp(error, "#{match[:code]} => #{match[:message]}")
end end
else
raise
end
end end
end end

View file

@ -197,7 +197,7 @@ module Fog
) )
begin begin
response = @connection.request({ @connection.request({
:body => body, :body => body,
:expects => 200, :expects => 200,
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' }, :headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
@ -207,23 +207,27 @@ module Fog
:parser => parser :parser => parser
}) })
rescue Excon::Errors::HTTPStatusError => error rescue Excon::Errors::HTTPStatusError => error
if match = error.response.body.match(/(?:.*<Code>(.*)<\/Code>)(?:.*<Message>(.*)<\/Message>)/m) match = Fog::AWS::Errors.match_error(error)
case match[1].split('.').last if match.empty?
when 'DBInstanceNotFound', 'DBParameterGroupNotFound', 'DBSnapshotNotFound', 'DBSecurityGroupNotFound' case error.message
raise Fog::AWS::RDS::NotFound.slurp(error, match[2]) when 'Not Found'
when 'DBParameterGroupAlreadyExists' raise Fog::AWS::RDS::NotFound.slurp(error, 'RDS Instance not found')
raise Fog::AWS::RDS::IdentifierTaken.slurp(error, match[2])
when 'AuthorizationAlreadyExists'
raise Fog::AWS::RDS::AuthorizationAlreadyExists.slurp(error, match[2])
else else
raise raise
end end
else else
raise raise case match[:code]
when 'DBInstanceNotFound', 'DBParameterGroupNotFound', 'DBSnapshotNotFound', 'DBSecurityGroupNotFound'
Fog::AWS::RDS::NotFound.slurp(error, match[:message])
when 'DBParameterGroupAlreadyExists'
Fog::AWS::RDS::IdentifierTaken.slurp(error, match[:message])
when 'AuthorizationAlreadyExists'
Fog::AWS::RDS::AuthorizationAlreadyExists.slurp(error, match[:message])
else
Fog::AWS::RDS::Error.slurp(error, "#{match[:code]} => #{match[:message]}")
end
end end
end end
response
end end
end end

View file

@ -102,7 +102,7 @@ module Fog
) )
begin begin
response = @connection.request({ @connection.request({
:body => body, :body => body,
:expects => 200, :expects => 200,
:idempotent => idempotent, :idempotent => idempotent,
@ -111,23 +111,17 @@ module Fog
:method => 'POST', :method => 'POST',
:parser => parser :parser => parser
}) })
response
rescue Excon::Errors::HTTPStatusError => error rescue Excon::Errors::HTTPStatusError => error
if match = error.message.match(/(?:.*<Code>(.*)<\/Code>)(?:.*<Message>(.*)<\/Message>)/m) match = Fog::AWS::Errors.match_error(error)
case match[1] raise if match.empty?
when 'EntityAlreadyExists', 'KeyPairMismatch', 'LimitExceeded', 'MalformedCertificate', 'ValidationError' raise case match[:code]
raise Fog::AWS::STS.const_get(match[1]).slurp(error, match[2]) when 'EntityAlreadyExists', 'KeyPairMismatch', 'LimitExceeded', 'MalformedCertificate', 'ValidationError'
else Fog::AWS::STS.const_get(match[:code]).slurp(error, match[:message])
raise Fog::AWS::STS::Error.slurp(error, "#{match[1]} => #{match[2]}") if match[1] else
raise Fog::AWS::STS::Error.slurp(error, "#{match[:code]} => #{match[:message]}")
end end
else
raise
end
end end
end end
end end