refacto: better error management
This commit is contained in:
parent
b6c74b0e83
commit
2ad4e4c01f
3 changed files with 38 additions and 19 deletions
|
@ -3,6 +3,11 @@ class Champs::SiretController < ApplicationController
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@champ = policy_scope(Champ).find(params[:champ_id])
|
@champ = policy_scope(Champ).find(params[:champ_id])
|
||||||
@siret = @champ.fetch_etablissement!(read_param_value(@champ.input_name, 'value'), current_user)
|
|
||||||
|
if @champ.fetch_etablissement!(read_param_value(@champ.input_name, 'value'), current_user)
|
||||||
|
@siret = @champ.etablissement.siret
|
||||||
|
else
|
||||||
|
@siret = @champ.etablissement_fetch_error_key
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,34 +1,38 @@
|
||||||
module SiretChampEtablissementFetchableConcern
|
module SiretChampEtablissementFetchableConcern
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
def fetch_etablissement!(siret, user)
|
attr_reader :etablissement_fetch_error_key
|
||||||
return clear if siret.empty?
|
|
||||||
return clear(error: :invalid) unless Siret.new(siret: siret).valid? # i18n-tasks-use t('errors.messages.invalid_siret')
|
|
||||||
return clear(error: :not_found) unless (etablissement = APIEntrepriseService.create_etablissement(self, siret, user.id)) # i18n-tasks-use t('errors.messages.siret_not_found')
|
|
||||||
|
|
||||||
return update_etablissement!(etablissement)
|
def fetch_etablissement!(siret, user)
|
||||||
|
return clear_etablissement!(:empty) if siret.empty?
|
||||||
|
return clear_etablissement!(:invalid) unless Siret.new(siret: siret).valid? # i18n-tasks-use t('errors.messages.invalid_siret')
|
||||||
|
return clear_etablissement!(:not_found) unless (etablissement = APIEntrepriseService.create_etablissement(self, siret, user&.id)) # i18n-tasks-use t('errors.messages.siret_not_found')
|
||||||
|
|
||||||
|
update!(value: siret, etablissement: etablissement)
|
||||||
rescue => error
|
rescue => error
|
||||||
if error.try(:network_error?) && !APIEntrepriseService.api_up?
|
if error.try(:network_error?) && !APIEntrepriseService.api_up?
|
||||||
# TODO: notify ops
|
# TODO: notify ops
|
||||||
etablissement = APIEntrepriseService.create_etablissement_as_degraded_mode(self, siret, user.id)
|
update!(
|
||||||
return update_etablissement!(etablissement, error: :api_entreprise_down)
|
value: siret,
|
||||||
|
etablissement: APIEntrepriseService.create_etablissement_as_degraded_mode(self, siret, user.id)
|
||||||
|
)
|
||||||
|
@etablissement_fetch_error_key = :api_entreprise_down
|
||||||
|
false
|
||||||
else
|
else
|
||||||
Sentry.capture_exception(error, extra: { dossier_id: dossier_id, siret: siret })
|
Sentry.capture_exception(error, extra: { dossier_id: dossier_id, siret: siret })
|
||||||
return clear(error: :network_error) # i18n-tasks-use t('errors.messages.siret_network_error')
|
clear_etablissement!(:network_error) # i18n-tasks-use t('errors.messages.siret_network_error')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def update_etablissement!(etablissement, error: nil)
|
def clear_etablissement!(error_key)
|
||||||
update!(value: etablissement.siret, etablissement: etablissement)
|
@etablissement_fetch_error_key = error_key
|
||||||
error.presence || etablissement.siret
|
|
||||||
end
|
|
||||||
|
|
||||||
def clear(error: nil)
|
|
||||||
etablissement_to_destroy = etablissement
|
etablissement_to_destroy = etablissement
|
||||||
update!(etablissement: nil)
|
update!(etablissement: nil)
|
||||||
etablissement_to_destroy&.destroy
|
etablissement_to_destroy&.destroy
|
||||||
error.presence
|
|
||||||
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,13 +20,18 @@ RSpec.describe SiretChampEtablissementFetchableConcern do
|
||||||
|
|
||||||
it { expect { fetch_etablissement! }.to change { Etablissement.count }.by(-1) }
|
it { expect { fetch_etablissement! }.to change { Etablissement.count }.by(-1) }
|
||||||
|
|
||||||
it { expect(fetch_etablissement!).to eq(error) }
|
it { expect(fetch_etablissement!).to eq(false) }
|
||||||
|
|
||||||
|
it 'populates the etablissement_fetch_error_key' do
|
||||||
|
fetch_etablissement!
|
||||||
|
expect(champ.etablissement_fetch_error_key).to eq(error)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the SIRET is empty' do
|
context 'when the SIRET is empty' do
|
||||||
let(:siret) { '' }
|
let(:siret) { '' }
|
||||||
|
|
||||||
it_behaves_like 'an error occured', nil
|
it_behaves_like 'an error occured', :empty
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the SIRET is invalid' do
|
context 'when the SIRET is invalid' do
|
||||||
|
@ -63,7 +68,12 @@ RSpec.describe SiretChampEtablissementFetchableConcern do
|
||||||
|
|
||||||
it { expect { fetch_etablissement! }.to change { Etablissement.count }.by(1) }
|
it { expect { fetch_etablissement! }.to change { Etablissement.count }.by(1) }
|
||||||
|
|
||||||
it { expect(fetch_etablissement!).to eq(:api_entreprise_down) }
|
it { expect(fetch_etablissement!).to eq(false) }
|
||||||
|
|
||||||
|
it 'populates the etablissement_fetch_error_key' do
|
||||||
|
fetch_etablissement!
|
||||||
|
expect(champ.etablissement_fetch_error_key).to eq(:api_entreprise_down)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the SIRET is valid but unknown' do
|
context 'when the SIRET is valid but unknown' do
|
||||||
|
@ -86,7 +96,7 @@ RSpec.describe SiretChampEtablissementFetchableConcern do
|
||||||
|
|
||||||
it { expect { fetch_etablissement! }.to change { Etablissement.count }.by(1) }
|
it { expect { fetch_etablissement! }.to change { Etablissement.count }.by(1) }
|
||||||
|
|
||||||
it { expect(fetch_etablissement!).to eq(siret) }
|
it { expect(fetch_etablissement!).to eq(true) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue