Previously, rest-client raised exceptions for HTTP errors, but
using OAuth would not cause the same behavior. Now HTTP errors lead
to exceptions in both cases.
Partial fix for
https://bugzilla.redhat.com/show_bug.cgi?id=784879
---
.../model/warehouse/warehouse_client.rb | 23 +++++++++++++------
.../model/warehouse/warehouse_model.rb | 10 +-------
2 files changed, 17 insertions(+), 16 deletions(-)
diff --git a/lib/aeolus_image/model/warehouse/warehouse_client.rb
b/lib/aeolus_image/model/warehouse/warehouse_client.rb
index 96565fb..4279365 100644
--- a/lib/aeolus_image/model/warehouse/warehouse_client.rb
+++ b/lib/aeolus_image/model/warehouse/warehouse_client.rb
@@ -145,15 +145,24 @@ module Aeolus
# Using restclient seems to break OAuth POSTs, so use the oauth gem directly if
we're using OAuth:
if @token
# TODO - I'm not sure how to pass :headers through here, but we don't
actually use them anywhere
- result = @token.request(opts[:method], (@uri + path).to_s,
opts[:content].to_s).body
- else
+ response = @token.request(opts[:method], (@uri + path).to_s,
opts[:content].to_s)
+ # Errors don't cause exceptions like they do with RestClient, so detect
and raise them:
+ if response.is_a?(Net::HTTPSuccess)
+ result = response.body
+ # Translate a few common errors to their RestClient counterparts that
we're used to checking for:
+ elsif response.is_a?(Net::HTTPNotFound)
+ raise RestClient::ResourceNotFound
+ elsif response.is_a?(Net::HTTPInternalServerError)
+ raise RestClient::InternalServerError
+ # Otherwise, raise the error:
+ else
+ response.error!
+ end
+ else # no @token -- use RestClient
result = RestClient::Request.execute(:method => opts[:method], :url =>
@uri + path, :payload => opts[:content], :headers => opts[:headers])
end
-
- return Nokogiri::XML result unless opts[:plain]
- return result
+ return opts[:plain] ? result : Nokogiri::XML(result)
end
-
end
class Client
@@ -163,7 +172,7 @@ module Aeolus
end
def create_bucket(bucket)
- @connection.do_request "/#{bucket}", :method => :put rescue
RestClient::InternalServerError
+ @connection.do_request("/#{bucket}", :method => :put) rescue
RestClient::InternalServerError
Bucket.new(bucket, @connection)
end
diff --git a/lib/aeolus_image/model/warehouse/warehouse_model.rb
b/lib/aeolus_image/model/warehouse/warehouse_model.rb
index dae77f8..dd494d6 100644
--- a/lib/aeolus_image/model/warehouse/warehouse_model.rb
+++ b/lib/aeolus_image/model/warehouse/warehouse_model.rb
@@ -168,15 +168,7 @@ module Aeolus
def create!(key, body, attributes)
self.set_warehouse_and_bucket if self.bucket.nil?
unless self.warehouse.buckets.include?(self.bucket.name)
- begin
- self.bucket = self.warehouse.create_bucket(self.bucket.name)
- rescue
- # there was an iwhd bug ago - it wasn't possible to find out
- # if a bucket exists or not, then code 500 was returned.
- # I'm not sure if it's already fixed or not so I'm wrapping
it
- # with rescue block. If exception is raised we can assume
- # that bucket already exists.
- end
+ self.bucket = self.warehouse.create_bucket(self.bucket.name)
end
obj = self.bucket.create_object(key, body, attributes)
self.new(obj)
--
1.7.6.5