Merge pull request #5771 from betagouv/less_api_entreprise_errors
Less api entreprise errors
This commit is contained in:
commit
db412b5854
8 changed files with 67 additions and 23 deletions
|
@ -105,7 +105,7 @@ module Users
|
||||||
sanitized_siret = siret_model.siret
|
sanitized_siret = siret_model.siret
|
||||||
begin
|
begin
|
||||||
etablissement = ApiEntrepriseService.create_etablissement(@dossier, sanitized_siret, current_user.id)
|
etablissement = ApiEntrepriseService.create_etablissement(@dossier, sanitized_siret, current_user.id)
|
||||||
rescue ApiEntreprise::API::RequestFailed
|
rescue ApiEntreprise::API::RequestFailed, ApiEntreprise::API::BadGateway
|
||||||
return render_siret_error(t('errors.messages.siret_network_error'))
|
return render_siret_error(t('errors.messages.siret_network_error'))
|
||||||
end
|
end
|
||||||
if etablissement.nil?
|
if etablissement.nil?
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
class ApiEntreprise::AttestationSocialeJob < ApiEntreprise::Job
|
class ApiEntreprise::AttestationSocialeJob < ApiEntreprise::Job
|
||||||
retry_on ApiEntreprise::API::ServiceUnavailable, wait: 1.day
|
|
||||||
|
|
||||||
def perform(etablissement_id, procedure_id)
|
def perform(etablissement_id, procedure_id)
|
||||||
etablissement = Etablissement.find(etablissement_id)
|
etablissement = Etablissement.find(etablissement_id)
|
||||||
etablissement_params = ApiEntreprise::AttestationSocialeAdapter.new(etablissement.siret, procedure_id).to_params
|
etablissement_params = ApiEntreprise::AttestationSocialeAdapter.new(etablissement.siret, procedure_id).to_params
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
class ApiEntreprise::Job < ApplicationJob
|
class ApiEntreprise::Job < ApplicationJob
|
||||||
queue_as :api_entreprise
|
queue_as :api_entreprise
|
||||||
|
|
||||||
|
retry_on ApiEntreprise::API::ServiceUnavailable,
|
||||||
|
ApiEntreprise::API::BadGateway,
|
||||||
|
wait: 1.day
|
||||||
|
|
||||||
DEFAULT_MAX_ATTEMPTS_API_ENTREPRISE_JOBS = 5
|
DEFAULT_MAX_ATTEMPTS_API_ENTREPRISE_JOBS = 5
|
||||||
|
|
||||||
# If by the time the job runs the Etablissement has been deleted
|
# If by the time the job runs the Etablissement has been deleted
|
||||||
|
|
|
@ -21,6 +21,9 @@ class ApiEntreprise::API
|
||||||
class BadFormatRequest < StandardError
|
class BadFormatRequest < StandardError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class BadGateway < StandardError
|
||||||
|
end
|
||||||
|
|
||||||
class ServiceUnavailable < StandardError
|
class ServiceUnavailable < StandardError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -97,10 +100,19 @@ class ApiEntreprise::API
|
||||||
raise ResourceNotFound, "url: #{url}"
|
raise ResourceNotFound, "url: #{url}"
|
||||||
elsif response.code == 400
|
elsif response.code == 400
|
||||||
raise BadFormatRequest, "url: #{url}"
|
raise BadFormatRequest, "url: #{url}"
|
||||||
|
elsif response.code == 502
|
||||||
|
raise BadGateway, "url: #{url}"
|
||||||
elsif response.code == 503
|
elsif response.code == 503
|
||||||
raise ServiceUnavailable, "url: #{url}"
|
raise ServiceUnavailable, "url: #{url}"
|
||||||
else
|
else
|
||||||
raise RequestFailed, "HTTP Error Code: #{response.code} for #{url}\nheaders: #{response.headers}\nbody: #{response.body}"
|
raise RequestFailed,
|
||||||
|
<<~TEXT
|
||||||
|
HTTP Error Code: #{response.code} for #{url}
|
||||||
|
headers: #{response.headers}
|
||||||
|
body: #{response.body}
|
||||||
|
curl message: #{response.return_message}
|
||||||
|
timeout: #{response.timed_out?}
|
||||||
|
TEXT
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -23,20 +23,4 @@ RSpec.describe ApiEntreprise::AttestationSocialeJob, type: :job do
|
||||||
subject
|
subject
|
||||||
expect(Etablissement.find(etablissement.id).entreprise_attestation_sociale).to be_attached
|
expect(Etablissement.find(etablissement.id).entreprise_attestation_sociale).to be_attached
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when ApiEntreprise::API::ServiceUnavailable is raised' do
|
|
||||||
# https://api.rubyonrails.org/classes/ActiveJob/Exceptions/ClassMethods.html#method-i-retry_on
|
|
||||||
# retry on will try 5 times and then bubble up the error
|
|
||||||
it 'makes 5 attempts' do
|
|
||||||
assert_performed_jobs 5 do
|
|
||||||
ServiceUnavailableJob.perform_later rescue ApiEntreprise::API::ServiceUnavailable
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ServiceUnavailableJob < ApiEntreprise::AttestationSocialeJob
|
|
||||||
def perform
|
|
||||||
raise ApiEntreprise::API::ServiceUnavailable
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
46
spec/jobs/api_entreprise/job_spec.rb
Normal file
46
spec/jobs/api_entreprise/job_spec.rb
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
include ActiveJob::TestHelper
|
||||||
|
|
||||||
|
RSpec.describe ApiEntreprise::Job, type: :job do
|
||||||
|
# https://api.rubyonrails.org/classes/ActiveJob/Exceptions/ClassMethods.html#method-i-retry_on
|
||||||
|
context 'when an exception is raised' do
|
||||||
|
subject do
|
||||||
|
assert_performed_jobs(try) do
|
||||||
|
ExceptionJob.perform_later(error) rescue StandardError
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when it is a service_unavaible' do
|
||||||
|
let(:error) { :standard_error }
|
||||||
|
let(:try) { 1 }
|
||||||
|
|
||||||
|
it { subject }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when it is a service_unavaible' do
|
||||||
|
let(:error) { :service_unavaible }
|
||||||
|
let(:try) { 5 }
|
||||||
|
|
||||||
|
it { subject }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when it is a bad gateway' do
|
||||||
|
let(:error) { :bad_gateway }
|
||||||
|
let(:try) { 5 }
|
||||||
|
|
||||||
|
it { subject }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ExceptionJob < ApiEntreprise::Job
|
||||||
|
def perform(exception)
|
||||||
|
case exception
|
||||||
|
when :service_unavaible
|
||||||
|
raise ApiEntreprise::API::ServiceUnavailable
|
||||||
|
when :bad_gateway
|
||||||
|
raise ApiEntreprise::API::BadGateway
|
||||||
|
else
|
||||||
|
raise StandardError
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -12,13 +12,13 @@ describe ApiEntreprise::API do
|
||||||
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the service is unavailable' do
|
context 'when the service throws a bad gateaway exception' do
|
||||||
let(:siren) { '111111111' }
|
let(:siren) { '111111111' }
|
||||||
let(:status) { 502 }
|
let(:status) { 502 }
|
||||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_unavailable.json') }
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_unavailable.json') }
|
||||||
|
|
||||||
it 'raises ApiEntreprise::API::RequestFailed' do
|
it 'raises ApiEntreprise::API::RequestFailed' do
|
||||||
expect { subject }.to raise_error(ApiEntreprise::API::RequestFailed)
|
expect { subject }.to raise_error(ApiEntreprise::API::BadGateway)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@ describe ApiEntreprise::EntrepriseAdapter do
|
||||||
|
|
||||||
context "when the service is unavailable" do
|
context "when the service is unavailable" do
|
||||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_unavailable.json') }
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_unavailable.json') }
|
||||||
let(:status) { 502 }
|
let(:status) { 500 }
|
||||||
|
|
||||||
it 'raises an exception' do
|
it 'raises an exception' do
|
||||||
expect { subject }.to raise_error(ApiEntreprise::API::RequestFailed)
|
expect { subject }.to raise_error(ApiEntreprise::API::RequestFailed)
|
||||||
|
|
Loading…
Reference in a new issue