feat(graphql): global not found error handling
This commit is contained in:
parent
fbae6d941d
commit
f70532a844
3 changed files with 41 additions and 9 deletions
|
@ -25,8 +25,6 @@ class API::V2::Schema < GraphQL::Schema
|
||||||
|
|
||||||
def self.object_from_id(id, ctx)
|
def self.object_from_id(id, ctx)
|
||||||
ApplicationRecord.record_from_typed_id(id)
|
ApplicationRecord.record_from_typed_id(id)
|
||||||
rescue => e
|
|
||||||
raise GraphQL::ExecutionError.new(e.message, extensions: { code: :not_found })
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.resolve_type(type_definition, object, ctx)
|
def self.resolve_type(type_definition, object, ctx)
|
||||||
|
@ -129,6 +127,10 @@ class API::V2::Schema < GraphQL::Schema
|
||||||
super
|
super
|
||||||
end
|
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
|
class Timeout < GraphQL::Schema::Timeout
|
||||||
def handle_timeout(error, query)
|
def handle_timeout(error, query)
|
||||||
Sentry.capture_exception(error, extra: query.context.query_info)
|
Sentry.capture_exception(error, extra: query.context.query_info)
|
||||||
|
|
|
@ -26,13 +26,11 @@ module Types
|
||||||
demarche_number = demarche.number.presence || ApplicationRecord.id_from_typed_id(demarche.id)
|
demarche_number = demarche.number.presence || ApplicationRecord.id_from_typed_id(demarche.id)
|
||||||
Procedure
|
Procedure
|
||||||
.includes(draft_revision: :procedure, published_revision: :procedure)
|
.includes(draft_revision: :procedure, published_revision: :procedure)
|
||||||
.find_by(id: demarche_number)
|
.find(demarche_number)
|
||||||
end
|
end
|
||||||
|
|
||||||
def demarche(number:)
|
def demarche(number:)
|
||||||
Procedure.for_api_v2.find(number)
|
Procedure.for_api_v2.find(number)
|
||||||
rescue => e
|
|
||||||
raise GraphQL::ExecutionError.new(e.message, extensions: { code: :not_found })
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def dossier(number:)
|
def dossier(number:)
|
||||||
|
@ -42,14 +40,10 @@ module Types
|
||||||
Dossier.visible_by_administration.for_api_v2.find(number)
|
Dossier.visible_by_administration.for_api_v2.find(number)
|
||||||
end
|
end
|
||||||
DossierPreloader.load_one(dossier)
|
DossierPreloader.load_one(dossier)
|
||||||
rescue => e
|
|
||||||
raise GraphQL::ExecutionError.new(e.message, extensions: { code: :not_found })
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def groupe_instructeur(number:)
|
def groupe_instructeur(number:)
|
||||||
GroupeInstructeur.for_api_v2.find(number)
|
GroupeInstructeur.for_api_v2.find(number)
|
||||||
rescue => e
|
|
||||||
raise GraphQL::ExecutionError.new(e.message, extensions: { code: :not_found })
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.accessible?(context)
|
def self.accessible?(context)
|
||||||
|
|
|
@ -76,6 +76,15 @@ describe API::V2::GraphqlController do
|
||||||
expect(gql_data[:dossier][:demandeur][:prenom]).to eq(dossier.individual.prenom)
|
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
|
context 'with entreprise' do
|
||||||
let(:procedure) { create(:procedure, :published, :with_service, administrateurs: [admin], types_de_champ_public:) }
|
let(:procedure) { create(:procedure, :published, :with_service, administrateurs: [admin], types_de_champ_public:) }
|
||||||
let(:dossier) { create(:dossier, :en_construction, :with_entreprise, procedure: procedure) }
|
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
|
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
|
context 'include Dossiers' do
|
||||||
let(:variables) { { demarcheNumber: procedure.id, includeDossiers: true } }
|
let(:variables) { { demarcheNumber: procedure.id, includeDossiers: true } }
|
||||||
|
|
||||||
|
@ -182,6 +200,15 @@ describe API::V2::GraphqlController do
|
||||||
expect(gql_data[:groupeInstructeur][:dossiers]).to be_nil
|
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
|
context 'include Dossiers' do
|
||||||
let(:variables) { { groupeInstructeurNumber: groupe_instructeur.id, includeDossiers: true } }
|
let(:variables) { { groupeInstructeurNumber: groupe_instructeur.id, includeDossiers: true } }
|
||||||
|
|
||||||
|
@ -207,6 +234,15 @@ describe API::V2::GraphqlController do
|
||||||
}
|
}
|
||||||
end
|
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
|
context 'find by id' do
|
||||||
let(:variables) { { demarche: { id: procedure.to_typed_id } } }
|
let(:variables) { { demarche: { id: procedure.to_typed_id } } }
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue