From 8c5f87c9d4118849bc2783db59997322b4064318 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Thu, 8 Mar 2018 16:30:07 +0100 Subject: [PATCH] Humanized error messages created by failing elasticsearch requests. --- lib/search_index_backend.rb | 80 ++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 9 deletions(-) diff --git a/lib/search_index_backend.rb b/lib/search_index_backend.rb index b12a5ee74..147bbce75 100644 --- a/lib/search_index_backend.rb +++ b/lib/search_index_backend.rb @@ -26,8 +26,13 @@ info about used search index machine } ) Rails.logger.info "# #{response.code}" - raise "Unable to process GET at #{url}\n#{response.inspect}" if !response.success? - response.data + return response.data if response.success? + + raise humanized_error( + verb: 'GET', + url: url, + response: response, + ) end =begin @@ -75,7 +80,12 @@ update processors Rails.logger.info "# #{response.code}" next if response.success? next if response.code.to_s == '404' - raise "Unable to process DELETE at #{url}\n#{response.inspect}" + + raise humanized_error( + verb: 'DELETE', + url: url, + response: response, + ) end Rails.logger.info "# curl -X PUT \"#{url}\" \\" Rails.logger.debug "-d '#{data.to_json}'" @@ -93,7 +103,13 @@ update processors ) Rails.logger.info "# #{response.code}" next if response.success? - raise "Unable to process PUT at #{url}\n#{response.inspect}" + + raise humanized_error( + verb: 'PUT', + url: url, + payload: item, + response: response, + ) end end true @@ -156,7 +172,13 @@ create/update/delete index ) Rails.logger.info "# #{response.code}" return true if response.success? - raise "Unable to process PUT at #{url}\n#{response.inspect}" + + raise humanized_error( + verb: 'PUT', + url: url, + payload: data[:data], + response: response, + ) end =begin @@ -188,7 +210,13 @@ add new object to search index ) Rails.logger.info "# #{response.code}" return true if response.success? - raise "Unable to process POST at #{url} (size: #{data.to_json.bytesize / 1024 / 1024}M)\n#{response.inspect}" + + raise humanized_error( + verb: 'POST', + url: url, + payload: data, + response: response, + ) end =begin @@ -219,7 +247,13 @@ remove whole data from index Rails.logger.info "# #{response.code}" return true if response.success? return true if response.code.to_s == '400' - Rails.logger.info "NOTICE: can't delete index #{url}: " + response.inspect + + humanized_error = humanized_error( + verb: 'DELETE', + url: url, + response: response, + ) + Rails.logger.info "NOTICE: can't delete index: #{humanized_error}" false end @@ -326,7 +360,12 @@ return search result Rails.logger.info "# #{response.code}" if !response.success? - Rails.logger.error "ERROR: POST on #{url}\n#{response.inspect}" + Rails.logger.error humanized_error( + verb: 'GET', + url: url, + payload: data, + response: response, + ) return [] end data = response.data @@ -419,7 +458,12 @@ get count of tickets and tickets which match on selector Rails.logger.info "# #{response.code}" if !response.success? - raise "Unable to process POST at #{url}\n#{response.inspect}" + raise humanized_error( + verb: 'GET', + url: url, + payload: data, + response: response, + ) end Rails.logger.debug response.data.to_json @@ -546,4 +590,22 @@ return true if backend is configured url end + def self.humanized_error(verb:, url:, payload: nil, response:) + prefix = "Unable to process #{verb} request to elasticsearch URL '#{url}'." + suffix = "\n\nResponse:\n#{response.inspect}\n\nPayload:\n#{payload.inspect}" + + if payload.respond_to?(:to_json) + suffix += "\n\nPayload size: #{payload.to_json.bytesize / 1024 / 1024}M" + end + + message = if response&.error&.match?('Connection refused') + "Elasticsearch is not reachable, probably because it's not running or even installed." + elsif url.end_with?('pipeline/zammad-attachment') && response.code == 400 + 'The installed attachment plugin could not handle the request payload. Ensure that the correct attachment plugin is installed (5.6 => ingest-attachment, 2.4 - 5.5 => mapper-attachments).' + else + 'Check the response and payload for detailed information: ' + end + + "#{prefix}#{message}#{suffix}" + end end