Skip to content

Commit

Permalink
gracefully handle query parser errors. #175
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jan 3, 2019
1 parent f1e4393 commit 3f24e9c
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 222 deletions.
68 changes: 38 additions & 30 deletions app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,48 @@ def index
response = Client.query(params[:query], year: params[:year], provider_id: params[:provider_id], software: params[:software], page: page, sort: sort)
end

total = response.results.total
total_pages = page[:size] > 0 ? (total.to_f / page[:size]).ceil : 0
years = total > 0 ? facet_by_year(response.response.aggregations.years.buckets) : nil
providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
software = total > 0 ? facet_by_software(response.response.aggregations.software.buckets) : nil
begin
total = response.results.total
total_pages = page[:size] > 0 ? (total.to_f / page[:size]).ceil : 0
years = total > 0 ? facet_by_year(response.response.aggregations.years.buckets) : nil
providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
software = total > 0 ? facet_by_software(response.response.aggregations.software.buckets) : nil

@clients = response.results.results
@clients = response.results.results

options = {}
options[:meta] = {
total: total,
"totalPages" => total_pages,
page: page[:number],
years: years,
providers: providers,
software: software
}.compact

options[:links] = {
self: request.original_url,
next: @clients.blank? ? nil : request.base_url + "/clients?" + {
query: params[:query],
"provider-id" => params[:provider_id],
software: params[:software],
year: params[:year],
fields: params[:fields],
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
options = {}
options[:meta] = {
total: total,
"totalPages" => total_pages,
page: page[:number],
years: years,
providers: providers,
software: software
}.compact
options[:include] = @include
options[:is_collection] = true

render json: ClientSerializer.new(@clients, options).serialized_json, status: :ok
options[:links] = {
self: request.original_url,
next: @clients.blank? ? nil : request.base_url + "/clients?" + {
query: params[:query],
"provider-id" => params[:provider_id],
software: params[:software],
year: params[:year],
fields: params[:fields],
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
}.compact
options[:include] = @include
options[:is_collection] = true

render json: ClientSerializer.new(@clients, options).serialized_json, status: :ok
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Bugsnag.notify(exception)

message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason")

render json: { "errors" => { "title" => message }}.to_json, status: :bad_request
end
end

def show
Expand Down
64 changes: 36 additions & 28 deletions app/controllers/data_centers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,46 @@ def index
response = Client.query(params[:query], year: params[:year], provider_id: params[:member_id], fields: params[:fields], page: page, sort: sort)
end

total = response.results.total
total_pages = page[:size] > 0 ? (total.to_f / page[:size]).ceil : 0
years = total > 0 ? facet_by_year(response.response.aggregations.years.buckets) : nil
providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
begin
total = response.results.total
total_pages = page[:size] > 0 ? (total.to_f / page[:size]).ceil : 0
years = total > 0 ? facet_by_year(response.response.aggregations.years.buckets) : nil
providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil

@clients = response.results.results
@clients = response.results.results

options = {}
options[:meta] = {
total: total,
"total-pages" => total_pages,
page: page[:number],
years: years,
members: providers
}.compact
options = {}
options[:meta] = {
total: total,
"total-pages" => total_pages,
page: page[:number],
years: years,
members: providers
}.compact

options[:links] = {
self: request.original_url,
next: @clients.blank? ? nil : request.base_url + "/data-centers?" + {
query: params[:query],
"member-id" => params[:member_id],
year: params[:year],
fields: params[:fields],
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
}.compact
options[:include] = @include
options[:is_collection] = true
options[:links] = nil
options[:links] = {
self: request.original_url,
next: @clients.blank? ? nil : request.base_url + "/data-centers?" + {
query: params[:query],
"member-id" => params[:member_id],
year: params[:year],
fields: params[:fields],
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
}.compact
options[:include] = @include
options[:is_collection] = true
options[:links] = nil

render json: DataCenterSerializer.new(@clients, options).serialized_json, status: :ok
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Bugsnag.notify(exception)

render json: DataCenterSerializer.new(@clients, options).serialized_json, status: :ok
message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason")

render json: { "errors" => { "title" => message }}.to_json, status: :bad_request
end
end

def show
Expand Down
138 changes: 73 additions & 65 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,73 +134,81 @@ def index
sort: sort)
end

total = response.results.total
total_pages = page[:size] > 0 ? ([total.to_f, 10000].min / page[:size]).ceil : 0

states = total > 0 ? facet_by_key(response.response.aggregations.states.buckets) : nil
resource_types = total > 0 ? facet_by_resource_type(response.response.aggregations.resource_types.buckets) : nil
created = total > 0 ? facet_by_year(response.response.aggregations.created.buckets) : nil
registered = total > 0 ? facet_by_year(response.response.aggregations.registered.buckets) : nil
providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
clients = total > 0 ? facet_by_client(response.response.aggregations.clients.buckets) : nil
prefixes = total > 0 ? facet_by_key(response.response.aggregations.prefixes.buckets) : nil
schema_versions = total > 0 ? facet_by_schema(response.response.aggregations.schema_versions.buckets) : nil
sources = total > 0 ? facet_by_key(response.response.aggregations.sources.buckets) : nil
link_checks_status = total > 0 ? facet_by_cumulative_year(response.response.aggregations.link_checks_status.buckets) : nil
links_with_schema_org = total > 0 ? facet_by_cumulative_year(response.response.aggregations.link_checks_has_schema_org.buckets) : nil
link_checks_schema_org_id = total > 0 ? response.response.aggregations.link_checks_schema_org_id.value : nil
link_checks_dc_identifier = total > 0 ? response.response.aggregations.link_checks_dc_identifier.value : nil
link_checks_citation_doi = total > 0 ? response.response.aggregations.link_checks_citation_doi.value : nil
links_checked = total > 0 ? response.response.aggregations.links_checked.value : nil

respond_to do |format|
format.json do
@dois = response.results.results
options = {}
options[:meta] = {
total: total,
"totalPages" => total_pages,
page: page[:number],
states: states,
"resourceTypes" => resource_types,
created: created,
registered: registered,
providers: providers,
clients: clients,
prefixes: prefixes,
"schemaVersions" => schema_versions,
sources: sources,
"linkChecksStatus" => link_checks_status,
"linksChecked" => links_checked,
"linksWithSchemaOrg" => links_with_schema_org,
"linkChecksSchemaOrgId" => link_checks_schema_org_id,
"linkChecksDcIdentifier" => link_checks_dc_identifier,
"linkChecksCitationDoi" => link_checks_citation_doi
}.compact

options[:links] = {
self: request.original_url,
next: @dois.blank? ? nil : request.base_url + "/dois?" + {
query: params[:query],
"provider-id" => params[:provider_id],
"client-id" => params[:client_id],
fields: params[:fields],
"page[cursor]" => Array.wrap(@dois.last[:sort]).first,
"page[size]" => params.dig(:page, :size) }.compact.to_query
begin
total = response.results.total
total_pages = page[:size] > 0 ? ([total.to_f, 10000].min / page[:size]).ceil : 0

states = total > 0 ? facet_by_key(response.response.aggregations.states.buckets) : nil
resource_types = total > 0 ? facet_by_resource_type(response.response.aggregations.resource_types.buckets) : nil
created = total > 0 ? facet_by_year(response.response.aggregations.created.buckets) : nil
registered = total > 0 ? facet_by_year(response.response.aggregations.registered.buckets) : nil
providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
clients = total > 0 ? facet_by_client(response.response.aggregations.clients.buckets) : nil
prefixes = total > 0 ? facet_by_key(response.response.aggregations.prefixes.buckets) : nil
schema_versions = total > 0 ? facet_by_schema(response.response.aggregations.schema_versions.buckets) : nil
sources = total > 0 ? facet_by_key(response.response.aggregations.sources.buckets) : nil
link_checks_status = total > 0 ? facet_by_cumulative_year(response.response.aggregations.link_checks_status.buckets) : nil
links_with_schema_org = total > 0 ? facet_by_cumulative_year(response.response.aggregations.link_checks_has_schema_org.buckets) : nil
link_checks_schema_org_id = total > 0 ? response.response.aggregations.link_checks_schema_org_id.value : nil
link_checks_dc_identifier = total > 0 ? response.response.aggregations.link_checks_dc_identifier.value : nil
link_checks_citation_doi = total > 0 ? response.response.aggregations.link_checks_citation_doi.value : nil
links_checked = total > 0 ? response.response.aggregations.links_checked.value : nil

respond_to do |format|
format.json do
@dois = response.results.results
options = {}
options[:meta] = {
total: total,
"totalPages" => total_pages,
page: page[:number],
states: states,
"resourceTypes" => resource_types,
created: created,
registered: registered,
providers: providers,
clients: clients,
prefixes: prefixes,
"schemaVersions" => schema_versions,
sources: sources,
"linkChecksStatus" => link_checks_status,
"linksChecked" => links_checked,
"linksWithSchemaOrg" => links_with_schema_org,
"linkChecksSchemaOrgId" => link_checks_schema_org_id,
"linkChecksDcIdentifier" => link_checks_dc_identifier,
"linkChecksCitationDoi" => link_checks_citation_doi
}.compact
options[:include] = @include
options[:is_collection] = true
options[:params] = {
:current_ability => current_ability,
}

render json: DoiSerializer.new(@dois, options).serialized_json, status: :ok
end
format.citation do
# fetch formatted citations
render citation: response.records.to_a, style: params[:style] || "apa", locale: params[:locale] || "en-US"

options[:links] = {
self: request.original_url,
next: @dois.blank? ? nil : request.base_url + "/dois?" + {
query: params[:query],
"provider-id" => params[:provider_id],
"client-id" => params[:client_id],
fields: params[:fields],
"page[cursor]" => Array.wrap(@dois.last[:sort]).first,
"page[size]" => params.dig(:page, :size) }.compact.to_query
}.compact
options[:include] = @include
options[:is_collection] = true
options[:params] = {
:current_ability => current_ability,
}

render json: DoiSerializer.new(@dois, options).serialized_json, status: :ok
end
format.citation do
# fetch formatted citations
render citation: response.records.to_a, style: params[:style] || "apa", locale: params[:locale] || "en-US"
end
format.any(:bibtex, :citeproc, :codemeta, :crosscite, :datacite, :datacite_json, :jats, :ris, :schema_org) { render request.format.to_sym => response.records.to_a }
end
format.any(:bibtex, :citeproc, :codemeta, :crosscite, :datacite, :datacite_json, :jats, :ris, :schema_org) { render request.format.to_sym => response.records.to_a }
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Bugsnag.notify(exception)

message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason")

render json: { "errors" => { "title" => message }}.to_json, status: :bad_request
end
end
end
Expand Down
76 changes: 42 additions & 34 deletions app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,52 @@ def index
response = Provider.query(params[:query], all_members: true, year: params[:year], region: params[:region], organization_type: params[:organization_type], focus_area: params[:focus_area], fields: params[:fields], page: page, sort: sort)
end

total = response.results.total
total_pages = page[:size] > 0 ? (total.to_f / page[:size]).ceil : 0
years = total > 0 ? facet_by_year(response.response.aggregations.years.buckets) : nil
regions = total > 0 ? facet_by_region(response.response.aggregations.regions.buckets) : nil
organization_types = total > 0 ? facet_by_key(response.response.aggregations.organization_types.buckets) : nil
focus_areas = total > 0 ? facet_by_key(response.response.aggregations.focus_areas.buckets) : nil
begin
total = response.results.total
total_pages = page[:size] > 0 ? (total.to_f / page[:size]).ceil : 0
years = total > 0 ? facet_by_year(response.response.aggregations.years.buckets) : nil
regions = total > 0 ? facet_by_region(response.response.aggregations.regions.buckets) : nil
organization_types = total > 0 ? facet_by_key(response.response.aggregations.organization_types.buckets) : nil
focus_areas = total > 0 ? facet_by_key(response.response.aggregations.focus_areas.buckets) : nil

@members = response.results.results
@members = response.results.results

options = {}
options[:meta] = {
total: total,
"total-pages" => total_pages,
page: page[:number],
years: years,
regions: regions,
"organization-types" => organization_types,
"focus-areas" => focus_areas
}.compact

options[:links] = {
self: request.original_url,
next: @members.blank? ? nil : request.base_url + "/members?" + {
query: params[:query],
year: params[:year],
region: params[:region],
"organization-type" => params[:organization_type],
"focus-area" => params[:focus_area],
fields: params[:fields],
"page[number]" => params.dig(:page, :number),
"page[size]" => params.dig(:page, :size),
sort: sort }.compact.to_query
options = {}
options[:meta] = {
total: total,
"total-pages" => total_pages,
page: page[:number],
years: years,
regions: regions,
"organization-types" => organization_types,
"focus-areas" => focus_areas
}.compact
options[:include] = @include
options[:is_collection] = true
options[:links] = nil

render json: MemberSerializer.new(@members, options).serialized_json, status: :ok
options[:links] = {
self: request.original_url,
next: @members.blank? ? nil : request.base_url + "/members?" + {
query: params[:query],
year: params[:year],
region: params[:region],
"organization-type" => params[:organization_type],
"focus-area" => params[:focus_area],
fields: params[:fields],
"page[number]" => params.dig(:page, :number),
"page[size]" => params.dig(:page, :size),
sort: sort }.compact.to_query
}.compact
options[:include] = @include
options[:is_collection] = true
options[:links] = nil

render json: MemberSerializer.new(@members, options).serialized_json, status: :ok
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Bugsnag.notify(exception)

message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason")

render json: { "errors" => { "title" => message }}.to_json, status: :bad_request
end
end

def show
Expand Down
Loading

0 comments on commit 3f24e9c

Please sign in to comment.