From 0098e948a70cb2074dcc59a5669a9a7c9fa15f95 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Fri, 10 May 2019 23:20:43 +0200 Subject: [PATCH] handle not found errors. #268 --- app/graphql/lupo_schema.rb | 20 +++++++++- app/graphql/types/query_type.rb | 68 +++++++++++++++++---------------- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/app/graphql/lupo_schema.rb b/app/graphql/lupo_schema.rb index 1545a1c3e..0be676f59 100644 --- a/app/graphql/lupo_schema.rb +++ b/app/graphql/lupo_schema.rb @@ -5,5 +5,23 @@ class LupoSchema < GraphQL::Schema max_depth 5 # mutation(Types::MutationType) - query(Types::QueryType) + query(QueryType) +end + +GraphQL::Errors.configure(LupoSchema) do + rescue_from ActiveRecord::RecordNotFound do |exception| + GraphQL::ExecutionError.new("Record not found") + end + + rescue_from ActiveRecord::RecordInvalid do |exception| + GraphQL::ExecutionError.new(exception.record.errors.full_messages.join("\n")) + end + + # rescue_from StandardError do |exception| + # GraphQL::ExecutionError.new("Please try to execute the query for this field later") + # end + + # rescue_from CustomError do |exception, object, arguments, context| + # nil + # end end diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index dcf7a2b7d..168271005 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -72,7 +72,10 @@ def funders(query: nil, first: nil) end def funder(id:) - Funder.find_by_id(id).first + result = Funder.find_by_id(id).first + fail ActiveRecord::RecordNotFound if result.nil? + + result end field :researcher, ResearcherType, null: false do @@ -80,7 +83,10 @@ def funder(id:) end def researcher(id:) - Researcher.find_by_id(id).first + result = Researcher.find_by_id(id).first + fail ActiveRecord::RecordNotFound if result.nil? + + result end field :organizations, [OrganizationType], null: false do @@ -97,7 +103,10 @@ def organizations(query: nil, first: nil) end def organization(id:) - Organization.find_by_id(id).first + result = Organization.find_by_id(id).first + fail ActiveRecord::RecordNotFound if result.nil? + + result end field :datasets, [DatasetType], null: false do @@ -114,8 +123,7 @@ def datasets(query: nil, first: nil) end def dataset(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :publications, [PublicationType], null: false do @@ -132,8 +140,7 @@ def publications(query: nil, first: nil) end def publication(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :audiovisuals, [AudiovisualType], null: false do @@ -150,8 +157,7 @@ def audiovisuals(query: nil, first: nil) end def audiovisual(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :collections, [CollectionType], null: false do @@ -168,8 +174,7 @@ def collections(query: nil, first: nil) end def collection(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :data_papers, [DataPaperType], null: false do @@ -186,8 +191,7 @@ def data_papers(query: nil, first: nil) end def data_paper(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :events, [EventType], null: false do @@ -204,8 +208,7 @@ def events(query: nil, first: nil) end def event(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :images, [ImageType], null: false do @@ -222,8 +225,7 @@ def images(query: nil, first: nil) end def image(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :interactive_resources, [InteractiveResourceType], null: false do @@ -240,8 +242,7 @@ def interactive_resources(query: nil, first: nil) end def interactive_resource(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :models, [ModelType], null: false do @@ -258,8 +259,7 @@ def models(query: nil, first: nil) end def model(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :physical_objects, [PhysicalObjectType], null: false do @@ -276,8 +276,7 @@ def physical_objects(query: nil, first: nil) end def physical_object(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :services, [ServiceType], null: false do @@ -294,8 +293,7 @@ def services(query: nil, first: nil) end def service(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :softwares, [SoftwareType], null: false do @@ -312,8 +310,7 @@ def softwares(query: nil, first: nil) end def software(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :sounds, [SoundType], null: false do @@ -330,8 +327,7 @@ def sounds(query: nil, first: nil) end def sound(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :workflows, [WorkflowType], null: false do @@ -348,8 +344,7 @@ def workflows(query: nil, first: nil) end def workflow(id:) - doi = doi_from_url(id) - Doi.find_by_id(doi).first + set_doi(id) end field :others, [OtherType], null: false do @@ -366,8 +361,17 @@ def others(query: nil, first: nil) end def other(id:) + set_doi(id) + end + + def set_doi(id) doi = doi_from_url(id) - Doi.find_by_id(doi).first + fail ActiveRecord::RecordNotFound if doi.nil? + + result = Doi.find_by_id(doi).first + fail ActiveRecord::RecordNotFound if result.nil? + + result end def doi_from_url(url)