From 8607e235449271d563c4cb785227c2bfaa69c632 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Thu, 13 Feb 2020 23:24:06 +0100 Subject: [PATCH] properly handle slack api errors. #416 --- app/jobs/handle_job.rb | 2 +- app/jobs/url_job.rb | 4 ++-- app/models/concerns/helpable.rb | 12 +++++++++--- app/models/concerns/mailable.rb | 2 ++ 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/jobs/handle_job.rb b/app/jobs/handle_job.rb index 4890fb5c9..77812dca0 100644 --- a/app/jobs/handle_job.rb +++ b/app/jobs/handle_job.rb @@ -12,7 +12,7 @@ def perform(doi_id) if doi.present? doi.register_url else - Rails.logger.error "[Handle] Error updating URL for DOI " + doi_id + ": not found" + Rails.logger.info "[Handle] Error updating URL for DOI " + doi_id + ": not found." end end end diff --git a/app/jobs/url_job.rb b/app/jobs/url_job.rb index 3a09cca9d..1971d1be3 100644 --- a/app/jobs/url_job.rb +++ b/app/jobs/url_job.rb @@ -25,10 +25,10 @@ def perform(doi_id) Rails.logger.info "[Handle] URL #{url} set for DOI #{doi.doi}." unless Rails.env.test? else - Rails.logger.error "[Handle] Error updating URL for DOI #{doi.doi}: URL not found." unless Rails.env.test? + Rails.logger.info "[Handle] Error updating URL for DOI #{doi.doi}: URL not found." unless Rails.env.test? end else - Rails.logger.error "[Handle] Error updating URL for DOI #{doi_id}: DOI not found" unless Rails.env.test? + Rails.logger.info "[Handle] Error updating URL for DOI #{doi_id}: DOI not found" unless Rails.env.test? end end end diff --git a/app/models/concerns/helpable.rb b/app/models/concerns/helpable.rb index 199c32cad..1e36a8c54 100644 --- a/app/models/concerns/helpable.rb +++ b/app/models/concerns/helpable.rb @@ -59,6 +59,8 @@ def register_url Rails.logger.info "[Handle] URL for DOI " + doi + " updated to " + url + "." unless Rails.env.test? self.__elasticsearch__.index_document + elsif response.status == 404 + Rails.logger.info "[Handle] Error updating URL for DOI " + doi + ": not found" else Rails.logger.error "[Handle] Error updating URL for DOI " + doi + ": " + response.body.inspect unless Rails.env.test? end @@ -171,14 +173,16 @@ def get_doi(options={}) url += "/api/handles/#{options[:doi]}" response = Maremma.get(url, username: "300%3A#{ENV['HANDLE_USERNAME']}", password: ENV['HANDLE_PASSWORD'], ssl_self_signed: true, timeout: 10) - if response.status != 200 + if response.status == 200 + response + elsif response.status == 404 + {} + else text = "Error " + response.body["errors"].inspect Rails.logger.error "[Handle] " + text User.send_notification_to_slack(text, title: "Error #{response.status.to_s}", level: "danger") unless Rails.env.test? end - - response end def delete_doi(options={}) @@ -190,6 +194,8 @@ def delete_doi(options={}) if response.status == 200 response + elsif response.status == 404 + {} else text = "Error " + response.body["errors"].inspect diff --git a/app/models/concerns/mailable.rb b/app/models/concerns/mailable.rb index 05b80f79c..08d5b7f1b 100644 --- a/app/models/concerns/mailable.rb +++ b/app/models/concerns/mailable.rb @@ -116,6 +116,8 @@ def send_notification_to_slack(text, options={}) icon_url: SLACK_ICON_URL response = notifier.ping attachments: [attachment] response.first.body + rescue Slack::Notifier::APIError => exception + Rails.logger.error exception.message unless exception.message.include?("HTTP Code 429") end end end