diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index d8a02e92b..b8c7123aa 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -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 diff --git a/app/controllers/data_centers_controller.rb b/app/controllers/data_centers_controller.rb index b55e64a19..ccf7b556f 100644 --- a/app/controllers/data_centers_controller.rb +++ b/app/controllers/data_centers_controller.rb @@ -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 diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index 440456ab0..0f6442aea 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -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 diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index 6b26f4abe..57d17cd63 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -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 diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index 8d77966c5..c62af5d6d 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -33,43 +33,51 @@ def index response = Provider.query(params[:query], year: params[:year], region: params[:region], organization_type: params[:organization_type], focus_area: params[:focus_area], 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 - @providers = response.results.results + @providers = response.results.results - options = {} - options[:meta] = { - total: total, - "totalPages" => total_pages, - page: page[:number], - years: years, - regions: regions, - "organizationTypes" => organization_types, - "focusAreas" => focus_areas - }.compact - - options[:links] = { - self: request.original_url, - next: @providers.blank? ? nil : request.base_url + "/providers?" + { - 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, + "totalPages" => total_pages, + page: page[:number], + years: years, + regions: regions, + "organizationTypes" => organization_types, + "focusAreas" => focus_areas }.compact - options[:include] = @include - options[:is_collection] = true - render json: ProviderSerializer.new(@providers, options).serialized_json, status: :ok + options[:links] = { + self: request.original_url, + next: @providers.blank? ? nil : request.base_url + "/providers?" + { + 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 + + render json: ProviderSerializer.new(@providers, 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 diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 568894575..74f7f97bf 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -47,42 +47,50 @@ def index sort: sort) end - total = response.results.total - total_pages = page[:size] > 0 ? ([total.to_f, 10000].min / page[:size]).ceil : 0 + begin + total = response.results.total + total_pages = page[:size] > 0 ? ([total.to_f, 10000].min / page[:size]).ceil : 0 + + resource_types = total > 0 ? facet_by_resource_type(response.response.aggregations.resource_types.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 + + @dois = response.results.results + + options = {} + options[:meta] = { + "resource-types" => resource_types, + registered: registered, + "data-centers" => clients, + total: total, + "total-pages" => total_pages, + page: page[:number] + }.compact - resource_types = total > 0 ? facet_by_resource_type(response.response.aggregations.resource_types.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 + options[:links] = { + self: request.original_url, + next: @dois.blank? ? nil : request.base_url + "/dois?" + { + query: params[:query], + "member-id" => params[:provider_id], + "data-center-id" => params[:client_id], + "page[size]" => params.dig(:page, :size) }.compact.to_query + }.compact + options[:include] = @include + options[:is_collection] = true + options[:links] = nil + options[:params] = { + :current_ability => current_ability, + } - @dois = response.results.results + render json: WorkSerializer.new(@dois, options).serialized_json, status: :ok + rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception + Bugsnag.notify(exception) - options = {} - options[:meta] = { - "resource-types" => resource_types, - registered: registered, - "data-centers" => clients, - total: total, - "total-pages" => total_pages, - page: page[:number] - }.compact - - options[:links] = { - self: request.original_url, - next: @dois.blank? ? nil : request.base_url + "/dois?" + { - query: params[:query], - "member-id" => params[:provider_id], - "data-center-id" => params[:client_id], - "page[size]" => params.dig(:page, :size) }.compact.to_query - }.compact - options[:include] = @include - options[:is_collection] = true - options[:links] = nil - options[:params] = { - :current_ability => current_ability, - } + message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason") - render json: WorkSerializer.new(@dois, options).serialized_json, status: :ok + render json: { "errors" => { "title" => message }}.to_json, status: :bad_request + end end def show