Merge branch 'master' into 3859-api-job-lock-method
[arvados.git] / apps / workbench / app / models / arvados_api_client.rb
index f36627f07b62e5034a05301791655bb7cf6ab881..78d3beef3c216161196c6451544c83ce5fbcc181 100644 (file)
@@ -8,10 +8,21 @@ class ArvadosApiClient
     def initialize(request_url, errmsg)
       @request_url = request_url
       @api_response ||= {}
+      errors = @api_response[:errors]
+      if not errors.is_a?(Array)
+        @api_response[:errors] = [errors || errmsg]
+      end
       super(errmsg)
     end
   end
 
+  class NoApiResponseException < ApiError
+    def initialize(request_url, exception)
+      @api_response_s = exception.to_s
+      super(request_url,
+            "#{exception.class.to_s} error connecting to API server")
+    end
+  end
 
   class InvalidApiResponseException < ApiError
     def initialize(request_url, api_response)
@@ -115,9 +126,11 @@ class ArvadosApiClient
 
     profile_checkpoint { "Prepare request #{url} #{query[:uuid]} #{query[:where]} #{query[:filters]}" }
     msg = @client_mtx.synchronize do
-      @api_client.post(url,
-                       query,
-                       header: header)
+      begin
+        @api_client.post(url, query, header: header)
+      rescue => exception
+        raise NoApiResponseException.new(url, exception)
+      end
     end
     profile_checkpoint 'API transaction'
 
@@ -129,7 +142,8 @@ class ArvadosApiClient
     if not resp.is_a? Hash
       raise InvalidApiResponseException.new(url, msg)
     elsif msg.status_code != 200
-      error_class = ERROR_CODE_CLASSES.fetch(msg.status_code, ApiError)
+      error_class = ERROR_CODE_CLASSES.fetch(msg.status_code,
+                                             ApiErrorResponseException)
       raise error_class.new(url, msg)
     end