feat(graphql): global not found error handling

This commit is contained in:
Paul Chavard 2023-04-18 10:08:18 +02:00
parent fbae6d941d
commit f70532a844
3 changed files with 41 additions and 9 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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 } } }