From 2222b6ff2da214ef5729fea1756065f80de399be Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Fri, 11 Oct 2024 16:13:28 +0200 Subject: [PATCH] [#10921] Extend error handling to cover new error codes --- app/lib/api_entreprise/api.rb | 2 +- .../api_entreprise/error_code_02002.json | 3 +++ .../api_entreprise/error_code_03002.json | 3 +++ spec/lib/api_entreprise/api_spec.rb | 20 +++++++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/files/api_entreprise/error_code_02002.json create mode 100644 spec/fixtures/files/api_entreprise/error_code_03002.json diff --git a/app/lib/api_entreprise/api.rb b/app/lib/api_entreprise/api.rb index 8b7fb819a..0c6b93bc4 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -141,7 +141,7 @@ class APIEntreprise::API def service_unavailable?(response) return true if response.code == 503 if response.code == 502 || response.code == 504 - parse_response_errors(response).any? { _1.is_a?(Hash) && ["01000", "01001", "01002"].include?(_1[:code]) } + parse_response_errors(response).any? { _1.is_a?(Hash) && ["01000", "01001", "01002", "02002", "03002"].include?(_1[:code]) } end end diff --git a/spec/fixtures/files/api_entreprise/error_code_02002.json b/spec/fixtures/files/api_entreprise/error_code_02002.json new file mode 100644 index 000000000..4faff400e --- /dev/null +++ b/spec/fixtures/files/api_entreprise/error_code_02002.json @@ -0,0 +1,3 @@ +{ + "errors": [{ "code": "02002" }] +} diff --git a/spec/fixtures/files/api_entreprise/error_code_03002.json b/spec/fixtures/files/api_entreprise/error_code_03002.json new file mode 100644 index 000000000..b6b4486dc --- /dev/null +++ b/spec/fixtures/files/api_entreprise/error_code_03002.json @@ -0,0 +1,3 @@ +{ + "errors": [{ "code": "03002" }] +} diff --git a/spec/lib/api_entreprise/api_spec.rb b/spec/lib/api_entreprise/api_spec.rb index fc9b4fbd5..e13d4f305 100644 --- a/spec/lib/api_entreprise/api_spec.rb +++ b/spec/lib/api_entreprise/api_spec.rb @@ -54,6 +54,26 @@ describe APIEntreprise::API do end end + context 'when the service reponds with 02002 code' do + let(:siren) { '111111111' } + let(:status) { 504 } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/error_code_02002.json').read } + + it 'raises APIEntreprise::API::Error::RequestFailed' do + expect { subject }.to raise_error(APIEntreprise::API::Error::ServiceUnavailable) + end + end + + context 'when the service reponds with 03002 code' do + let(:siren) { '111111111' } + let(:status) { 504 } + let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/error_code_03002.json').read } + + it 'raises APIEntreprise::API::Error::RequestFailed' do + expect { subject }.to raise_error(APIEntreprise::API::Error::ServiceUnavailable) + end + end + context 'when siren does not exist' do let(:siren) { '111111111' } let(:status) { 404 }