From f70532a8442696ddb27b982e831e0f8b99fc10c0 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 18 Apr 2023 10:08:18 +0200 Subject: [PATCH] feat(graphql): global not found error handling --- app/graphql/api/v2/schema.rb | 6 ++-- app/graphql/types/query_type.rb | 8 +---- .../graphql_controller_stored_queries_spec.rb | 36 +++++++++++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/app/graphql/api/v2/schema.rb b/app/graphql/api/v2/schema.rb index 1f418e377..25779ff04 100644 --- a/app/graphql/api/v2/schema.rb +++ b/app/graphql/api/v2/schema.rb @@ -25,8 +25,6 @@ class API::V2::Schema < GraphQL::Schema def self.object_from_id(id, ctx) ApplicationRecord.record_from_typed_id(id) - rescue => e - raise GraphQL::ExecutionError.new(e.message, extensions: { code: :not_found }) end def self.resolve_type(type_definition, object, ctx) @@ -129,6 +127,10 @@ class API::V2::Schema < GraphQL::Schema super end + rescue_from(ActiveRecord::RecordNotFound) do |_error, _object, _args, _ctx, field| + raise GraphQL::ExecutionError.new("#{field.type.unwrap.graphql_name} not found", extensions: { code: :not_found }) + end + class Timeout < GraphQL::Schema::Timeout def handle_timeout(error, query) Sentry.capture_exception(error, extra: query.context.query_info) diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 47b7c1b9f..044eae3a6 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -26,13 +26,11 @@ module Types demarche_number = demarche.number.presence || ApplicationRecord.id_from_typed_id(demarche.id) Procedure .includes(draft_revision: :procedure, published_revision: :procedure) - .find_by(id: demarche_number) + .find(demarche_number) end def demarche(number:) Procedure.for_api_v2.find(number) - rescue => e - raise GraphQL::ExecutionError.new(e.message, extensions: { code: :not_found }) end def dossier(number:) @@ -42,14 +40,10 @@ module Types Dossier.visible_by_administration.for_api_v2.find(number) end DossierPreloader.load_one(dossier) - rescue => e - raise GraphQL::ExecutionError.new(e.message, extensions: { code: :not_found }) end def groupe_instructeur(number:) GroupeInstructeur.for_api_v2.find(number) - rescue => e - raise GraphQL::ExecutionError.new(e.message, extensions: { code: :not_found }) end def self.accessible?(context) diff --git a/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb b/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb index 5dd0fcebb..7b790db02 100644 --- a/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb +++ b/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb @@ -76,6 +76,15 @@ describe API::V2::GraphqlController do expect(gql_data[:dossier][:demandeur][:prenom]).to eq(dossier.individual.prenom) } + context 'not found' do + let(:variables) { { dossierNumber: 0 } } + + it { + expect(gql_errors.first[:message]).to eq('Dossier not found') + expect(gql_errors.first[:extensions]).to eq({ code: 'not_found' }) + } + end + context 'with entreprise' do let(:procedure) { create(:procedure, :published, :with_service, administrateurs: [admin], types_de_champ_public:) } let(:dossier) { create(:dossier, :en_construction, :with_entreprise, procedure: procedure) } @@ -114,6 +123,15 @@ describe API::V2::GraphqlController do expect(gql_data[:demarche][:dossiers]).to be_nil } + context 'not found' do + let(:variables) { { demarcheNumber: 0 } } + + it { + expect(gql_errors.first[:message]).to eq('Demarche not found') + expect(gql_errors.first[:extensions]).to eq({ code: 'not_found' }) + } + end + context 'include Dossiers' do let(:variables) { { demarcheNumber: procedure.id, includeDossiers: true } } @@ -182,6 +200,15 @@ describe API::V2::GraphqlController do expect(gql_data[:groupeInstructeur][:dossiers]).to be_nil } + context 'not found' do + let(:variables) { { groupeInstructeurNumber: 0 } } + + it { + expect(gql_errors.first[:message]).to eq('GroupeInstructeurWithDossiers not found') + expect(gql_errors.first[:extensions]).to eq({ code: 'not_found' }) + } + end + context 'include Dossiers' do let(:variables) { { groupeInstructeurNumber: groupe_instructeur.id, includeDossiers: true } } @@ -207,6 +234,15 @@ describe API::V2::GraphqlController do } end + context 'not found' do + let(:variables) { { demarche: { number: 0 } } } + + it { + expect(gql_errors.first[:message]).to eq('DemarcheDescriptor not found') + expect(gql_errors.first[:extensions]).to eq({ code: 'not_found' }) + } + end + context 'find by id' do let(:variables) { { demarche: { id: procedure.to_typed_id } } }