From 7f81cba8afda0377fb6670de10ea56092f7bee57 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 6 Jul 2019 19:39:52 +0200 Subject: [PATCH] proper error message for dois not in crossref index. #303 --- Gemfile.lock | 2 +- app/controllers/dois_controller.rb | 3 + app/jobs/crossref_doi_by_id_job.rb | 2 + .../not_found_on_updating_the_record.yml | 86 +++++++++++++++++++ .../updates_the_record.yml | 86 +++++++++++++++++++ spec/requests/dois_spec.rb | 34 ++++++++ 6 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/vcr_cassettes/dois/POST_/dois/crossref_url_not_found/not_found_on_updating_the_record.yml create mode 100644 spec/fixtures/vcr_cassettes/dois/POST_/dois/crossref_url_not_found/updates_the_record.yml diff --git a/Gemfile.lock b/Gemfile.lock index 6c6c68152..fcd2ddaa1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -106,7 +106,7 @@ GEM latex-decode (~> 0.0) binding_of_caller (0.8.0) debug_inspector (>= 0.0.1) - bolognese (1.2.9) + bolognese (1.2.11) activesupport (>= 4.2.5, < 6) benchmark_methods (~> 0.7) bibtex-ruby (~> 4.1) diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index d739b08bf..c6ae4afd6 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -579,6 +579,9 @@ def safe_params p[:schemaVersion] = METADATA_FORMATS.include?(meta["from"]) ? LAST_SCHEMA_VERSION : p[:schemaVersion] xml = meta["string"] + # if metadata for DOIs from other registration agencies are not found + fail ActiveRecord::RecordNotFound if meta["state"] == "not_found" + read_attrs = [p[:creators], p[:contributors], p[:titles], p[:publisher], p[:publicationYear], p[:types], p[:descriptions], p[:container], p[:sizes], p[:formats], p[:version], p[:language], p[:dates], p[:identifiers], diff --git a/app/jobs/crossref_doi_by_id_job.rb b/app/jobs/crossref_doi_by_id_job.rb index 75bd58ef3..b2080b341 100644 --- a/app/jobs/crossref_doi_by_id_job.rb +++ b/app/jobs/crossref_doi_by_id_job.rb @@ -59,6 +59,8 @@ def perform(id, options={}) logger.info "DOI #{doi} record created." elsif response.status == 200 logger.info "DOI #{doi} record updated." + elsif response.status == 404 + logger.info "[Error parsing #{ra} DOI #{doi}]: not found." else logger.error "[Error parsing #{ra} DOI #{doi}]: " + response.body["errors"].inspect end diff --git a/spec/fixtures/vcr_cassettes/dois/POST_/dois/crossref_url_not_found/not_found_on_updating_the_record.yml b/spec/fixtures/vcr_cassettes/dois/POST_/dois/crossref_url_not_found/not_found_on_updating_the_record.yml new file mode 100644 index 000000000..bbd471f44 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/dois/POST_/dois/crossref_url_not_found/not_found_on_updating_the_record.yml @@ -0,0 +1,86 @@ +--- +http_interactions: +- request: + method: get + uri: https://doi.org/ra/10.3389 + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Mozilla/5.0 (compatible; Maremma/4.2.6; mailto:info@datacite.org) + Accept: + - text/html,application/json,application/xml;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5 + response: + status: + code: 200 + message: '' + headers: + Date: + - Sat, 06 Jul 2019 17:31:11 GMT + Content-Type: + - application/json;charset=UTF-8 + Content-Length: + - '54' + Connection: + - keep-alive + Set-Cookie: + - __cfduid=d39a0717ba9f5b32b07747ad180a521a11562434271; expires=Sun, 05-Jul-20 + 17:31:11 GMT; path=/; domain=.doi.org; HttpOnly + Expect-Ct: + - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + Server: + - cloudflare + Cf-Ray: + - 4f234f927d41c2a4-FRA + body: + encoding: ASCII-8BIT + string: |- + [ + { + "DOI": "10.3389", + "RA": "Crossref" + } + ] + http_version: + recorded_at: Sat, 06 Jul 2019 17:31:11 GMT +- request: + method: get + uri: https://api.crossref.org/works/10.3389/fmicb.2019.01425/transform/application/vnd.crossref.unixsd+xml + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Mozilla/5.0 (compatible; Maremma/4.2.6; mailto:info@datacite.org) + Accept: + - text/xml + response: + status: + code: 404 + message: Not Found + headers: + Content-Type: + - text/plain + Access-Control-Allow-Origin: + - "*" + Access-Control-Allow-Headers: + - X-Requested-With + Content-Length: + - '19' + Server: + - http-kit + Date: + - Sat, 06 Jul 2019 17:31:11 GMT + X-Rate-Limit-Limit: + - '50' + X-Rate-Limit-Interval: + - 1s + Connection: + - close + body: + encoding: ASCII-8BIT + string: Resource not found. + http_version: + recorded_at: Sat, 06 Jul 2019 17:31:11 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/dois/POST_/dois/crossref_url_not_found/updates_the_record.yml b/spec/fixtures/vcr_cassettes/dois/POST_/dois/crossref_url_not_found/updates_the_record.yml new file mode 100644 index 000000000..cf23f27a9 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/dois/POST_/dois/crossref_url_not_found/updates_the_record.yml @@ -0,0 +1,86 @@ +--- +http_interactions: +- request: + method: get + uri: https://doi.org/ra/10.3389 + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Mozilla/5.0 (compatible; Maremma/4.2.6; mailto:info@datacite.org) + Accept: + - text/html,application/json,application/xml;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5 + response: + status: + code: 200 + message: '' + headers: + Date: + - Sat, 06 Jul 2019 17:24:18 GMT + Content-Type: + - application/json;charset=UTF-8 + Content-Length: + - '54' + Connection: + - keep-alive + Set-Cookie: + - __cfduid=db77d6e6fbdf82e93e08708b40f828daa1562433858; expires=Sun, 05-Jul-20 + 17:24:18 GMT; path=/; domain=.doi.org; HttpOnly + Expect-Ct: + - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + Server: + - cloudflare + Cf-Ray: + - 4f23457d0b9dc29a-FRA + body: + encoding: ASCII-8BIT + string: |- + [ + { + "DOI": "10.3389", + "RA": "Crossref" + } + ] + http_version: + recorded_at: Sat, 06 Jul 2019 17:24:18 GMT +- request: + method: get + uri: https://api.crossref.org/works/10.3389/fmicb.2019.01425/transform/application/vnd.crossref.unixsd+xml + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Mozilla/5.0 (compatible; Maremma/4.2.6; mailto:info@datacite.org) + Accept: + - text/xml + response: + status: + code: 404 + message: Not Found + headers: + Content-Type: + - text/plain + Access-Control-Allow-Origin: + - "*" + Access-Control-Allow-Headers: + - X-Requested-With + Content-Length: + - '19' + Server: + - http-kit + Date: + - Sat, 06 Jul 2019 17:24:18 GMT + X-Rate-Limit-Limit: + - '50' + X-Rate-Limit-Interval: + - 1s + Connection: + - close + body: + encoding: ASCII-8BIT + string: Resource not found. + http_version: + recorded_at: Sat, 06 Jul 2019 17:24:18 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index e85f557c7..c8e4bafa1 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -984,6 +984,40 @@ end end + context 'crossref url not found', vcr: true do + let(:provider) { create(:provider, name: "Crossref", symbol: "CROSSREF", role_name: "ROLE_REGISTRATION_AGENCY") } + let(:client) { create(:client, provider: provider, name: "Crossref Citations", symbol: "CROSSREF.CITATIONS") } + + let(:xml) { Base64.strict_encode64("https://doi.org/10.3389/fmicb.2019.01425") } + let(:valid_attributes) do + { + "data" => { + "type" => "dois", + "attributes" => { + "xml" => xml, + "source" => "test", + "event" => "publish" + }, + "relationships" => { + "client" => { + "data" => { + "type" => "clients", + "id" => client.symbol.downcase + } + } + } + } + } + end + + it 'not found on updating the record' do + patch "/dois/10.3389/fmicb.2019.01425", valid_attributes, headers + + expect(last_response.status).to eq(404) + expect(json['errors']).to eq([{"status"=>"404", "title"=>"The resource you are looking for doesn't exist."}]) + end + end + context 'medra url', vcr: true do let(:provider) { create(:provider, name: "mEDRA", symbol: "MEDRA", role_name: "ROLE_REGISTRATION_AGENCY") } let(:client) { create(:client, provider: provider, name: "mEDRA Citations", symbol: "MEDRA.CITATIONS") }