Skip to content

Commit

Permalink
properly handle slack api errors. #416
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 13, 2020
1 parent 40237ce commit 8607e23
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/jobs/handle_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions app/jobs/url_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 9 additions & 3 deletions app/models/concerns/helpable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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={})
Expand All @@ -190,6 +194,8 @@ def delete_doi(options={})

if response.status == 200
response
elsif response.status == 404
{}
else
text = "Error " + response.body["errors"].inspect

Expand Down
2 changes: 2 additions & 0 deletions app/models/concerns/mailable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8607e23

Please sign in to comment.