From e0a0ee103a64e72b4800e64555036e59dc3ee70f Mon Sep 17 00:00:00 2001 From: Tycho Braams Date: Mon, 22 Aug 2022 23:01:18 +0200 Subject: [PATCH 1/3] Also catch faraday server errors --- lib/diplomat/query.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/diplomat/query.rb b/lib/diplomat/query.rb index 3cac851..834c434 100644 --- a/lib/diplomat/query.rb +++ b/lib/diplomat/query.rb @@ -32,7 +32,7 @@ def create(definition, options = {}) custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil @raw = send_post_request(@conn, ['/v1/query'], options, definition, custom_params) parse_body - rescue Faraday::ClientError + rescue Faraday::ClientError, Faraday::ServerError raise Diplomat::QueryAlreadyExists end From b68e95d74dec4735589a32b1b850109eacfcbfdc Mon Sep 17 00:00:00 2001 From: Tycho Braams Date: Tue, 23 Aug 2022 22:33:35 +0200 Subject: [PATCH 2/3] handle ServerError not existing for faraday < 1 --- lib/diplomat/query.rb | 2 +- lib/diplomat/rest_client.rb | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/diplomat/query.rb b/lib/diplomat/query.rb index 834c434..5f85267 100644 --- a/lib/diplomat/query.rb +++ b/lib/diplomat/query.rb @@ -32,7 +32,7 @@ def create(definition, options = {}) custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil @raw = send_post_request(@conn, ['/v1/query'], options, definition, custom_params) parse_body - rescue Faraday::ClientError, Faraday::ServerError + rescue *faraday_error_classes raise Diplomat::QueryAlreadyExists end diff --git a/lib/diplomat/rest_client.rb b/lib/diplomat/rest_client.rb index c8a30aa..38d5319 100644 --- a/lib/diplomat/rest_client.rb +++ b/lib/diplomat/rest_client.rb @@ -256,7 +256,7 @@ def send_get_request(connection, url, options, custom_params = nil) rest_options[:headers].map { |k, v| req.headers[k.to_sym] = v } unless rest_options[:headers].nil? req.options.timeout = options[:timeout] if options[:timeout] end - rescue Faraday::ClientError, Faraday::ServerError => e + rescue *faraday_error_classes => e resp = e.response if resp raise Diplomat::AclNotFound, e if resp[:status] == 403 && resp[:body] == 'ACL not found' @@ -304,6 +304,14 @@ def send_delete_request(connection, url, options, custom_params = nil) end end + # This method returns the correct Exception Classes depending on the Faraday version being installed + # see https://github.com/WeAreFarmGeek/diplomat/issues/227 + # + # @return [Array] Faraday error classes that should be caught + def faraday_error_classes + Faraday.const_defined?(:ServerError) ? [Faraday::ClientError, Faraday::ServerError] : [Faraday::ClientError] + end + # Mapping for valid key/value store transaction verbs and required parameters # # @return [Hash] valid key/store transaction verbs and required parameters From e3bb98b6329b8b469afb48c33075115ca0c7f36a Mon Sep 17 00:00:00 2001 From: Tycho Braams Date: Thu, 25 Aug 2022 09:17:07 +0200 Subject: [PATCH 3/3] ensure timeout errors are re-raised --- lib/diplomat/query.rb | 2 ++ lib/diplomat/rest_client.rb | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lib/diplomat/query.rb b/lib/diplomat/query.rb index 5f85267..3115905 100644 --- a/lib/diplomat/query.rb +++ b/lib/diplomat/query.rb @@ -32,6 +32,8 @@ def create(definition, options = {}) custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil @raw = send_post_request(@conn, ['/v1/query'], options, definition, custom_params) parse_body + rescue Faraday::TimeoutError => e + raise e rescue *faraday_error_classes raise Diplomat::QueryAlreadyExists end diff --git a/lib/diplomat/rest_client.rb b/lib/diplomat/rest_client.rb index 38d5319..07975f0 100644 --- a/lib/diplomat/rest_client.rb +++ b/lib/diplomat/rest_client.rb @@ -256,6 +256,8 @@ def send_get_request(connection, url, options, custom_params = nil) rest_options[:headers].map { |k, v| req.headers[k.to_sym] = v } unless rest_options[:headers].nil? req.options.timeout = options[:timeout] if options[:timeout] end + rescue Faraday::TimeoutError => e + raise e rescue *faraday_error_classes => e resp = e.response if resp