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 14, 2020
1 parent 8607e23 commit 339fcef
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
61 changes: 30 additions & 31 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,39 @@ class DoisController < ApplicationController
include Crosscitable
include MetricsHelper # mixes in your helper method as class method


prepend_before_action :authenticate_user!
before_action :set_include, only: [:index, :show, :create, :update]
before_action :set_raven_context, only: [:create, :update, :validate]

def index
sort = case params[:sort]
when "name" then { "doi" => { order: 'asc' }}
when "-name" then { "doi" => { order: 'desc' }}
when "created" then { created: { order: 'asc' }}
when "-created" then { created: { order: 'desc' }}
when "updated" then { updated: { order: 'asc' }}
when "-updated" then { updated: { order: 'desc' }}
when "published" then { published: { order: 'asc' }}
when "-published" then { published: { order: 'desc' }}
when "view-count" then { view_count: { order: 'asc' }}
when "-view-count" then { view_count: { order: 'desc' }}
when "download-count" then { download_count: { order: 'asc' }}
when "-download-count" then { download_count: { order: 'desc' }}
when "citation-count" then { citation_count: { order: 'asc' }}
when "-citation-count" then { citation_count: { order: 'desc' }}
when "relevance" then { "_score": { "order": "desc" }}
else { updated: { order: 'desc' }}
end
when "name" then { "doi" => { order: 'asc' }}
when "-name" then { "doi" => { order: 'desc' }}
when "created" then { created: { order: 'asc' }}
when "-created" then { created: { order: 'desc' }}
when "updated" then { updated: { order: 'asc' }}
when "-updated" then { updated: { order: 'desc' }}
when "published" then { published: { order: 'asc' }}
when "-published" then { published: { order: 'desc' }}
when "view-count" then { view_count: { order: 'asc' }}
when "-view-count" then { view_count: { order: 'desc' }}
when "download-count" then { download_count: { order: 'asc' }}
when "-download-count" then { download_count: { order: 'desc' }}
when "citation-count" then { citation_count: { order: 'asc' }}
when "-citation-count" then { citation_count: { order: 'desc' }}
when "relevance" then { "_score": { "order": "desc" }}
else { updated: { order: 'desc' }}
end

page = page_from_params(params)

sample_group_field = case params[:sample_group]
when "client" then "client_id"
when "data-center" then "client_id"
when "provider" then "provider_id"
when "resource-type" then "types.resourceTypeGeneral"
else nil
end
when "client" then "client_id"
when "data-center" then "client_id"
when "provider" then "provider_id"
when "resource-type" then "types.resourceTypeGeneral"
else nil
end

response = nil

Expand Down Expand Up @@ -419,7 +418,7 @@ def update
options[:params] = {
current_ability: current_ability,
detail: true,
affiliation: params[:affiliation]
affiliation: params[:affiliation],
}

render json: DoiSerializer.new(@doi, options).serialized_json, status: exists ? :ok : :created
Expand All @@ -431,7 +430,7 @@ def update

def undo
@doi = Doi.where(doi: safe_params[:doi]).first
fail ActiveRecord::RecordNotFound unless @doi.present?
fail ActiveRecord::RecordNotFound if @doi.blank?

authorize! :undo, @doi

Expand Down Expand Up @@ -482,13 +481,13 @@ def random

def get_url
@doi = Doi.where(doi: params[:id]).first
fail ActiveRecord::RecordNotFound unless @doi.present?
fail ActiveRecord::RecordNotFound if @doi.blank?

authorize! :get_url, @doi

if !@doi.is_registered_or_findable? || %w(europ crossref medra jalc kisti op).include?(@doi.provider_id) || %w(Crossref mEDRA).include?(@doi.agency)
url = @doi.url
head :no_content and return unless url.present?
head :no_content && return if url.blank?
else
response = @doi.get_url

Expand All @@ -512,12 +511,12 @@ def get_url
def get_dois
authorize! :get_urls, Doi

client = Client.where('datacentre.symbol = ?', current_user.uid.upcase).first
client = Client.where("datacentre.symbol = ?", current_user.uid.upcase).first
client_prefix = client.prefixes.first
head :no_content and return unless client_prefix.present?
head :no_content && return if client_prefix.blank?

dois = Doi.get_dois(prefix: client_prefix.prefix, username: current_user.uid.upcase, password: current_user.password)
if dois.length > 0
if dois.length.positive?
render json: { dois: dois }.to_json, status: :ok
else
head :no_content
Expand Down
28 changes: 14 additions & 14 deletions app/models/concerns/helpable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def register_url
self.__elasticsearch__.index_document
elsif response.status == 404
Rails.logger.info "[Handle] Error updating URL for DOI " + doi + ": not found"
elsif response.status == 408
Rails.logger.warn "[Handle] Error updating URL for DOI " + doi + ": timeout"
else
Rails.logger.error "[Handle] Error updating URL for DOI " + doi + ": " + response.body.inspect unless Rails.env.test?
end
Expand Down Expand Up @@ -135,21 +137,21 @@ def https_to_http(url)

module ClassMethods
def get_dois(options={})
return OpenStruct.new(body: { "errors" => [{ "title" => "Prefix missing" }] }) unless options[:prefix].present?
return OpenStruct.new(body: { "errors" => [{ "title" => "Prefix missing" }] }) if options[:prefix].blank?

count_url = ENV['HANDLE_URL'] + "/api/handles?prefix=#{options[:prefix]}&pageSize=0"
response = Maremma.get(count_url, username: "300%3A#{ENV['HANDLE_USERNAME']}", password: ENV['HANDLE_PASSWORD'], ssl_self_signed: true, timeout: 60)
count_url = ENV["HANDLE_URL"] + "/api/handles?prefix=#{options[:prefix]}&pageSize=0"
response = Maremma.get(count_url, username: "300%3A#{ENV['HANDLE_USERNAME']}", password: ENV["HANDLE_PASSWORD"], ssl_self_signed: true, timeout: 60)

total = response.body.dig("data", "totalCount").to_i
dois = []

if total > 0
# walk through paginated results
total_pages = (total.to_f / 1000).ceil

(0...total_pages).each do |page|
url = ENV['HANDLE_URL'] + "/api/handles?prefix=#{options[:prefix]}&page=#{page}&pageSize=1000"
response = Maremma.get(url, username: "300%3A#{ENV['HANDLE_USERNAME']}", password: ENV['HANDLE_PASSWORD'], ssl_self_signed: true, timeout: 60)
url = ENV["HANDLE_URL"] + "/api/handles?prefix=#{options[:prefix]}&page=#{page}&pageSize=1000"
response = Maremma.get(url, username: "300%3A#{ENV['HANDLE_USERNAME']}", password: ENV["HANDLE_PASSWORD"], ssl_self_signed: true, timeout: 60)
if response.status == 200
dois += (response.body.dig("data", "handles") || [])
else
Expand All @@ -167,16 +169,16 @@ def get_dois(options={})
end

def get_doi(options={})
return OpenStruct.new(body: { "errors" => [{ "title" => "DOI missing" }] }) unless options[:doi].present?
return OpenStruct.new(body: { "errors" => [{ "title" => "DOI missing" }] }) if options[:doi].blank?

url = Rails.env.production? ? "https://doi.org" : "https://handle.test.datacite.org"
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)
response = Maremma.get(url, username: "300%3A#{ENV['HANDLE_USERNAME']}", password: ENV["HANDLE_PASSWORD"], ssl_self_signed: true, timeout: 10)

if response.status == 200
response
elsif response.status == 404
{}
OpenStruct.new(status: 404, body: { "errors" => [{ "status" => 404, "title" => "Not found." }] })
else
text = "Error " + response.body["errors"].inspect

Expand All @@ -186,7 +188,7 @@ def get_doi(options={})
end

def delete_doi(options={})
return OpenStruct.new(body: { "errors" => [{ "title" => "DOI missing" }] }) unless options[:doi].present?
return OpenStruct.new(body: { "errors" => [{ "title" => "DOI missing" }] }) if options[:doi].blank?
return OpenStruct.new(body: { "errors" => [{ "title" => "Only DOIs with prefix 10.5072 can be deleted" }] }) unless options[:doi].start_with?("10.5072")

url = "#{ENV['HANDLE_URL']}/api/handles/#{options[:doi]}"
Expand All @@ -195,7 +197,7 @@ def delete_doi(options={})
if response.status == 200
response
elsif response.status == 404
{}
OpenStruct.new(status: 404, body: { "errors" => [{ "status" => 404, "title" => "Not found." }] })
else
text = "Error " + response.body["errors"].inspect

Expand All @@ -214,9 +216,7 @@ def parse_attributes(element, options={})
element.fetch( CGI.unescapeHTML(content), nil)
elsif element.is_a?(Array)
a = element.map { |e| e.is_a?(Hash) ? e.fetch( CGI.unescapeHTML(content), nil) : e }.uniq
a = options[:first] ? a.first : a.unwrap
else
nil
options[:first] ? a.first : a.unwrap
end
end
end
Expand Down

0 comments on commit 339fcef

Please sign in to comment.