From ddd2e2e7bc067afc6d7918ba062f8c0b682912f4 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Mon, 24 Aug 2020 08:27:17 +0200 Subject: [PATCH] handle orcid api errors gracefully --- app/models/person.rb | 10 ++++ .../Person/query/handle_errors_gracefully.yml | 51 +++++++++++++++++++ .../query_people_with_error/returns_error.yml | 51 +++++++++++++++++++ .../returns_people_information.yml | 51 +++++++++++++++++++ spec/graphql/types/person_type_spec.rb | 36 +++++++++++++ spec/models/person_spec.rb | 5 ++ 6 files changed, 204 insertions(+) create mode 100644 spec/fixtures/vcr_cassettes/Person/query/handle_errors_gracefully.yml create mode 100644 spec/fixtures/vcr_cassettes/PersonType/query_people_with_error/returns_error.yml create mode 100644 spec/fixtures/vcr_cassettes/PersonType/query_people_with_error/returns_people_information.yml diff --git a/app/models/person.rb b/app/models/person.rb index 430961b14..41102cd26 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -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") @@ -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 diff --git a/spec/fixtures/vcr_cassettes/Person/query/handle_errors_gracefully.yml b/spec/fixtures/vcr_cassettes/Person/query/handle_errors_gracefully.yml new file mode 100644 index 000000000..1b559fb0f --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Person/query/handle_errors_gracefully.yml @@ -0,0 +1,51 @@ +--- +http_interactions: +- request: + method: get + uri: https://pub.orcid.org/v3.0/expanded-search/?q=container.identifier:2658-719X&rows=25&start=0 + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Mozilla/5.0 (compatible; Maremma/4.7.2; mailto:info@datacite.org) + Accept: + - application/json;charset=UTF-8 + response: + status: + code: 500 + message: Internal Server Error + headers: + Server: + - nginx/1.16.1 + Cache-Control: + - no-cache, no-store, max-age=0, must-revalidate + Content-Type: + - application/json;charset=UTF-8 + Date: + - Mon, 24 Aug 2020 06:12:19 GMT + Expires: + - '0' + Pragma: + - no-cache + X-Xss-Protection: + - 1; mode=block + Access-Control-Allow-Origin: + - "*" + X-Content-Type-Options: + - nosniff + Connection: + - close + Set-Cookie: + - X-Mapping-fjhppofk=26484D0F5DA32D2D6AF64AA1C9DBBA16; path=/ + X-Frame-Options: + - DENY + body: + encoding: ASCII-8BIT + string: '{"response-code":500,"developer-message":"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","user-message":"Something went wrong + in ORCID.","error-code":9008,"more-info":"https://members.orcid.org/api/resources/troubleshooting"}' + http_version: null + recorded_at: Mon, 24 Aug 2020 06:12:19 GMT +recorded_with: VCR 5.1.0 diff --git a/spec/fixtures/vcr_cassettes/PersonType/query_people_with_error/returns_error.yml b/spec/fixtures/vcr_cassettes/PersonType/query_people_with_error/returns_error.yml new file mode 100644 index 000000000..210346843 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/PersonType/query_people_with_error/returns_error.yml @@ -0,0 +1,51 @@ +--- +http_interactions: +- request: + method: get + uri: https://pub.orcid.org/v3.0/expanded-search/?q=container.identifier:2658-719X&rows=25&start=0 + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Mozilla/5.0 (compatible; Maremma/4.7.2; mailto:info@datacite.org) + Accept: + - application/json;charset=UTF-8 + response: + status: + code: 500 + message: Internal Server Error + headers: + Server: + - nginx/1.16.1 + Cache-Control: + - no-cache, no-store, max-age=0, must-revalidate + Content-Type: + - application/json;charset=UTF-8 + Date: + - Mon, 24 Aug 2020 06:18:34 GMT + Expires: + - '0' + Pragma: + - no-cache + X-Xss-Protection: + - 1; mode=block + Access-Control-Allow-Origin: + - "*" + X-Content-Type-Options: + - nosniff + Connection: + - close + Set-Cookie: + - X-Mapping-fjhppofk=26484D0F5DA32D2D6AF64AA1C9DBBA16; path=/ + X-Frame-Options: + - DENY + body: + encoding: ASCII-8BIT + string: '{"response-code":500,"developer-message":"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","user-message":"Something went wrong + in ORCID.","error-code":9008,"more-info":"https://members.orcid.org/api/resources/troubleshooting"}' + http_version: null + recorded_at: Mon, 24 Aug 2020 06:18:34 GMT +recorded_with: VCR 5.1.0 diff --git a/spec/fixtures/vcr_cassettes/PersonType/query_people_with_error/returns_people_information.yml b/spec/fixtures/vcr_cassettes/PersonType/query_people_with_error/returns_people_information.yml new file mode 100644 index 000000000..9a7962ab3 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/PersonType/query_people_with_error/returns_people_information.yml @@ -0,0 +1,51 @@ +--- +http_interactions: +- request: + method: get + uri: https://pub.orcid.org/v3.0/expanded-search/?q=container.identifier:2658-719X&rows=25&start=0 + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Mozilla/5.0 (compatible; Maremma/4.7.2; mailto:info@datacite.org) + Accept: + - application/json;charset=UTF-8 + response: + status: + code: 500 + message: Internal Server Error + headers: + Server: + - nginx/1.16.1 + Cache-Control: + - no-cache, no-store, max-age=0, must-revalidate + Content-Type: + - application/json;charset=UTF-8 + Date: + - Mon, 24 Aug 2020 06:16:39 GMT + Expires: + - '0' + Pragma: + - no-cache + X-Xss-Protection: + - 1; mode=block + Access-Control-Allow-Origin: + - "*" + X-Content-Type-Options: + - nosniff + Connection: + - close + Set-Cookie: + - X-Mapping-fjhppofk=EDEB8B375DA428655747278237992826; path=/ + X-Frame-Options: + - DENY + body: + encoding: ASCII-8BIT + string: '{"response-code":500,"developer-message":"org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException + Full validation error: Error from server at http://solr-8-loc.orcid.org:8983/solr/profile: + undefined field container.identifier","user-message":"Something went wrong + in ORCID.","error-code":9008,"more-info":"https://members.orcid.org/api/resources/troubleshooting"}' + http_version: null + recorded_at: Mon, 24 Aug 2020 06:16:39 GMT +recorded_with: VCR 5.1.0 diff --git a/spec/graphql/types/person_type_spec.rb b/spec/graphql/types/person_type_spec.rb index 69c994dd4..f21721923 100644 --- a/spec/graphql/types/person_type_spec.rb +++ b/spec/graphql/types/person_type_spec.rb @@ -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 diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index f7003eb8d..71b28db4d 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -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