Skip to content

Commit

Permalink
handle orcid api errors gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Aug 24, 2020
1 parent f673737 commit ddd2e2e
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ def self.find_by_id(id)

url = "https://pub.orcid.org/v3.0/#{orcid}/person"
response = Maremma.get(url, accept: "json")

if response.status >= 405
message = response.body.dig("errors", 0, "title", "developer-message") || "Something went wrong in ORCID"
fail ::Faraday::ClientError, message
end

return {} if response.status != 200

message = response.body.dig("data")
Expand Down Expand Up @@ -58,6 +64,10 @@ def self.query(query, options={})
url = "https://pub.orcid.org/v3.0/expanded-search/?" + URI.encode_www_form(params)

response = Maremma.get(url, accept: "json")
if response.status >= 400
message = response.body.dig("errors", 0, "title", "developer-message") || "Something went wrong in ORCID"
fail ::Faraday::ClientError, message
end

return [] if response.status != 200

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions spec/graphql/types/person_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,40 @@
expect(person.fetch("alternateName")).to eq(["Nélida R. Villaseñor"])
end
end

describe "query people with error", elasticsearch: true, vcr: true do
let(:query) do
%(query {
people(query: "container.identifier:2658-719X") {
totalCount
pageInfo {
endCursor
hasNextPage
}
nodes {
id
name
givenName
familyName
alternateName
works {
totalCount
published {
id
title
count
}
}
}
}
})
end

it "returns error" do
response = LupoSchema.execute(query).as_json

expect(response.dig("data")).to be_nil
expect(response.dig("errors", 0, "message")).to eq("org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException Full validation error: Error from server at http://solr-loc.orcid.org/solr/profile: undefined field container.identifier")
end
end
end
5 changes: 5 additions & 0 deletions spec/models/person_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,10 @@
expect(person.identifiers).to be_empty
expect(person.country).to be_nil
end

it "handle errors gracefully" do
query = "container.identifier:2658-719X"
expect { Person.query(query) }.to raise_error(Faraday::ClientError, /Error from server/)
end
end
end

0 comments on commit ddd2e2e

Please sign in to comment.