From 6737dea852e354b89204f4f603b70a71c81e2ce6 Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Wed, 15 Mar 2023 11:25:01 -0400 Subject: [PATCH] Provide message when record not found Why are these changes being introduced: * when a recordId query was run but no record was found, the application through a 500 error * expected behavior would be to return a message to the user that the record they requested was not found Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/TIMX-197 How does this address that need: * This fixes a copy/paste error where we were looking for an ElasticSearch Not Found error rather than an OpenSearch Not Found error in the GraphQL for v2 logic --- app/graphql/types/query_type.rb | 2 +- .../controllers/graphql_controller_v2_test.rb | 16 ++++ .../graphql_v2_retrieve_not_found.yml | 76 +++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 test/vcr_cassettes/graphql_v2_retrieve_not_found.yml diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 5723773..4654a8e 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -38,7 +38,7 @@ def info def record_id(id:, index:) result = Retrieve.new.fetch(id, Timdex::OSClient, index) result['hits']['hits'].first['_source'] - rescue Elasticsearch::Transport::Transport::Errors::NotFound + rescue OpenSearch::Transport::Transport::Errors::NotFound raise GraphQL::ExecutionError, "Record '#{id}' not found" end diff --git a/test/controllers/graphql_controller_v2_test.rb b/test/controllers/graphql_controller_v2_test.rb index b6b7f69..5cfb9b5 100644 --- a/test/controllers/graphql_controller_v2_test.rb +++ b/test/controllers/graphql_controller_v2_test.rb @@ -331,6 +331,22 @@ def setup end end + test 'graphqlv2 retrieve with not found recordid' do + VCR.use_cassette('graphql v2 retrieve not found') do + post '/graphql', params: { query: '{ + recordId(id: "totallylegitrecordid") { + timdexRecordId + title + } + }' } + assert_equal(200, response.status) + json = JSON.parse(response.body) + assert_nil(json['data']) + assert_equal("Record 'totallylegitrecordid' not found", json['errors'].first['message']) + end + end + + test 'graphqlv2 holding location is not required' do VCR.use_cassette('graphql v2 location') do post '/graphql', params: { query: '{ diff --git a/test/vcr_cassettes/graphql_v2_retrieve_not_found.yml b/test/vcr_cassettes/graphql_v2_retrieve_not_found.yml new file mode 100644 index 0000000..54f38c1 --- /dev/null +++ b/test/vcr_cassettes/graphql_v2_retrieve_not_found.yml @@ -0,0 +1,76 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:9200/ + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - 'opensearch-ruby/2.1.0 (RUBY_VERSION: 3.1.2; darwin x86_64; Faraday v2.7.4)' + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=UTF-8 + Content-Length: + - '347' + body: + encoding: ASCII-8BIT + string: | + { + "name" : "abb77df940c2", + "cluster_name" : "docker-cluster", + "cluster_uuid" : "c-VRJvUhQEC0sXa8uIdOQw", + "version" : { + "distribution" : "opensearch", + "number" : "2.3.0", + "build_type" : "tar", + "build_hash" : "6f6e84ebc54af31a976f53af36a5c69d474a5140", + "build_date" : "2022-09-09T00:07:12.137133581Z", + "build_snapshot" : false, + "lucene_version" : "9.3.0", + "minimum_wire_compatibility_version" : "7.10.0", + "minimum_index_compatibility_version" : "7.0.0" + }, + "tagline" : "The OpenSearch Project: https://opensearch.org/" + } + recorded_at: Wed, 15 Mar 2023 15:20:12 GMT +- request: + method: post + uri: http://localhost:9200/timdex-prod/_search + body: + encoding: UTF-8 + string: '{"query":{"ids":{"values":["totallylegitrecordid"]}}}' + headers: + User-Agent: + - 'opensearch-ruby/2.1.0 (RUBY_VERSION: 3.1.2; darwin x86_64; Faraday v2.7.4)' + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=UTF-8 + Content-Length: + - '142' + body: + encoding: ASCII-8BIT + string: '{"took":47,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":0,"relation":"eq"},"max_score":null,"hits":[]}}' + recorded_at: Wed, 15 Mar 2023 15:20:12 GMT +recorded_with: VCR 6.1.0