# File lib/rhc/rest.rb, line 186 def generic_error(url) ServerErrorException.new( "The server did not respond correctly. This may be an issue " "with the server configuration or with your connection to the " "server (such as a Web proxy or firewall)." "#{RestClient.proxy.present? ? " Please verify that your proxy server is working correctly (#{RestClient.proxy}) and that you can access the OpenShift server #{url}" : "Please verify that you can access the OpenShift server #{url}"}", 129) end
@@headers = {:accept => "application/json;version=#{RHC::Rest::VERSION}"}
# File lib/rhc/rest.rb, line 92 def logger Logger.new(STDOUT) end
# File lib/rhc/rest.rb, line 150 def new_request(options) # user specified timeout takes presidence options[:timeout] = $rest_timeout || options[:timeout] options[:open_timeout] ||= 4 RestClient::Request.new options end
# File lib/rhc/rest.rb, line 96 def parse_response(response) result = RHC::Json.decode(response) type = result['type'] data = result['data'] case type when 'domains' domains = Array.new data.each do |domain_json| domains.push(Domain.new(domain_json, @debug)) end return domains when 'domain' return Domain.new(data, @debug) when 'applications' apps = Array.new data.each do |app_json| apps.push(Application.new(app_json, @debug)) end return apps when 'application' app = Application.new(data, @debug) result['messages'].each do |message| app.add_message(message['text']) if message['field'].nil? or message['field'] == 'result' end return app when 'cartridges' carts = Array.new data.each do |cart_json| carts.push(Cartridge.new(cart_json, @debug)) end return carts when 'cartridge' return Cartridge.new(data, @debug) when 'user' return User.new(data, @debug) when 'keys' keys = Array.new data.each do |key_json| keys.push(Key.new(key_json, @debug)) end return keys when 'key' return Key.new(data, @debug) when 'gear_groups' gears = Array.new data.each do |gear_json| gears.push(GearGroup.new(gear_json, @debug)) end return gears else data end end
# File lib/rhc/rest.rb, line 195 def process_error_response(response, url=nil) messages = [] parse_error = nil begin result = RHC::Json.decode(response) messages = Array(result['messages']) rescue => e logger.debug "Response did not include a message from server: #{e.message}" if @debug parse_error = generic_error(url) end case response.code when 401 raise UnAuthorizedException, "Not authenticated" when 403 messages.each do |message| if message['severity'].upcase == "ERROR" raise RequestDeniedException, message['text'] end end raise RequestDeniedException.new("Forbidden") when 404 messages.each do |message| if message['severity'].upcase == "ERROR" raise ResourceNotFoundException, message['text'] end end raise ResourceNotFoundException.new(url) when 409 messages.each do |message| if message['severity'] and message['severity'].upcase == "ERROR" raise ValidationException.new(message['text'], message['field'], message['exit_code']) end end when 422 e = nil messages.each do |message| if e and e.field == message["field"] e.message << " #{message["text"]}" else e = ValidationException.new(message["text"], message["field"], message["exit_code"]) end end raise e || parse_error || ValidationException.new('Not valid') when 400 messages.each do |message| if message['severity'].upcase == "ERROR" raise ClientErrorException, message['text'] end end when 500 messages.each do |message| if message['severity'].upcase == "ERROR" raise ServerErrorException.new(message['text'], message["exit_code"] ? message["exit_code"].to_i : nil) end end when 503 messages.each do |message| if message['severity'].upcase == "ERROR" raise ServiceUnavailableException, message['text'] end end raise ServiceUnavailableException else raise ServerErrorException, "Server returned an unexpected error code: #{response.code}" end raise parse_error || generic_error(url) end
# File lib/rhc/rest.rb, line 158 def request(request, &block) begin response = request.execute #set cookie rh_sso = response.cookies['rh_sso'] if not rh_sso.nil? @@headers["cookie"] = "rh_sso=#{rh_sso}" end if block_given? yield response else parse_response(response) unless response.nil? or response.code == 204 end rescue RestClient::RequestTimeout => e raise TimeoutException.new("Connection to server timed out. It is possible the operation finished without being able to report success. Use 'rhc domain show' or 'rhc app status' to check the status of your applications.") rescue RestClient::ServerBrokeConnection => e raise ConnectionException.new("Connection to server got interrupted: #{e.message}") rescue RestClient::ExceptionWithResponse => e process_error_response(e.response, request.url) rescue SocketError => e raise ConnectionException.new("Unable to connect to the server (#{e.message})." "#{RestClient.proxy.present? ? " Check that you have correctly specified your proxy server '#{RestClient.proxy}' as well as your OpenShift server '#{request.url}'." : " Check that you have correctly specified your OpenShift server '#{request.url}'."}") rescue => e logger.debug e.backtrace.join("\n ") if @debug raise ResourceAccessException.new("Failed to access resource: #{e.message}") end end