lib/stash/server.rb in face_control-0.9.0 vs lib/stash/server.rb in face_control-0.9.1
- old
+ new
@@ -1,10 +1,12 @@
require 'httparty'
require 'stash/repository'
module Stash
class Server
+ class CommunicationError < StandardError; end
+
attr_accessor :root_uri, :user, :password, :logger
def initialize(root_uri, user, password, logger = nil)
self.root_uri = root_uri
self.user = user
@@ -17,18 +19,22 @@
@repositories ||= {}
@repositories[key] ||= Repository.new(self, project_key, repository_slug)
end
def get(path)
- logged('Response') do
- HTTParty.get(endpoint + path, auth.merge(logging))
+ with_error_handling do
+ logged('Response') do
+ HTTParty.get(endpoint + path, auth.merge(logging))
+ end
end
end
def post(path, data)
- logged('Response') do
- HTTParty.post(endpoint + path, auth.merge(content_type).merge(body: data.to_json).merge(logging))
+ with_error_handling do
+ logged('Response') do
+ HTTParty.post(endpoint + path, auth.merge(content_type).merge(body: data.to_json).merge(logging))
+ end
end
end
private
@@ -69,9 +75,17 @@
logger: logger,
log_level: :debug
}
else
{}
+ end
+ end
+
+ def with_error_handling
+ yield.tap do |response|
+ unless response.success?
+ fail CommunicationError, 'Stash responded with an error'
+ end
end
end
end
end