From d39a70dce86644881a3177c99f358fdb46ad25ed Mon Sep 17 00:00:00 2001 From: Ivan Novosad Date: Fri, 23 Aug 2024 15:51:04 +0200 Subject: [PATCH] misc(netsuite): Process payload errors --- .../integrations/aggregator/base_service.rb | 6 ++-- .../error_payload_response.json | 9 +++++ .../contacts/create_service_spec.rb | 35 +++++++++++++++++++ .../contacts/update_service_spec.rb | 35 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/integration_aggregator/error_payload_response.json diff --git a/app/services/integrations/aggregator/base_service.rb b/app/services/integrations/aggregator/base_service.rb index a7a51d5e083..29fcf7e34f6 100644 --- a/app/services/integrations/aggregator/base_service.rb +++ b/app/services/integrations/aggregator/base_service.rb @@ -92,12 +92,14 @@ def secret_key def code(error) json = error.json_message - json['type'].presence || json.dig('error', 'code') + json['type'].presence || json.dig('error', 'payload', 'name').presence || json.dig('error', 'code') end def message(error) json = error.json_message - json.dig('payload', 'message').presence || json.dig('error', 'message') + json.dig('payload', 'message').presence || + json.dig('error', 'payload', 'message').presence || + json.dig('error', 'message') end end end diff --git a/spec/fixtures/integration_aggregator/error_payload_response.json b/spec/fixtures/integration_aggregator/error_payload_response.json new file mode 100644 index 00000000000..c9b90f1834c --- /dev/null +++ b/spec/fixtures/integration_aggregator/error_payload_response.json @@ -0,0 +1,9 @@ +{ + "error": { + "code": "action_script_runtime_error", + "payload": { + "name": "TypeError", + "message": "Please enter value(s) for: Company Name" + } + } +} diff --git a/spec/services/integrations/aggregator/contacts/create_service_spec.rb b/spec/services/integrations/aggregator/contacts/create_service_spec.rb index 972fd0d7ae8..fd7f126b5fc 100644 --- a/spec/services/integrations/aggregator/contacts/create_service_spec.rb +++ b/spec/services/integrations/aggregator/contacts/create_service_spec.rb @@ -255,6 +255,41 @@ end end + context 'when it is a server payload error' do + let(:error_code) { Faker::Number.between(from: 500, to: 599) } + let(:code) { 'TypeError' } + let(:message) { 'Please enter value(s) for: Company Name' } + + let(:body) do + path = Rails.root.join('spec/fixtures/integration_aggregator/error_payload_response.json') + File.read(path) + end + + it 'returns an error' do + result = service_call + + aggregate_failures do + expect(result).not_to be_success + expect(result.error.code).to eq(code) + expect(result.error.message).to eq("#{code}: #{message}") + end + end + + it 'delivers an error webhook' do + expect { service_call }.to enqueue_job(SendWebhookJob) + .with( + 'customer.accounting_provider_error', + customer, + provider: 'netsuite', + provider_code: integration.code, + provider_error: { + message:, + error_code: code + } + ) + end + end + context 'when it is a client error' do let(:error_code) { 404 } let(:code) { 'invalid_secret_key_format' } diff --git a/spec/services/integrations/aggregator/contacts/update_service_spec.rb b/spec/services/integrations/aggregator/contacts/update_service_spec.rb index 1221fbe3500..720912df3a8 100644 --- a/spec/services/integrations/aggregator/contacts/update_service_spec.rb +++ b/spec/services/integrations/aggregator/contacts/update_service_spec.rb @@ -190,6 +190,41 @@ end end + context 'when it is a server payload error' do + let(:error_code) { Faker::Number.between(from: 500, to: 599) } + let(:code) { 'TypeError' } + let(:message) { 'Please enter value(s) for: Company Name' } + + let(:body) do + path = Rails.root.join('spec/fixtures/integration_aggregator/error_payload_response.json') + File.read(path) + end + + it 'returns an error' do + result = service_call + + aggregate_failures do + expect(result).not_to be_success + expect(result.error.code).to eq(code) + expect(result.error.message).to eq("#{code}: #{message}") + end + end + + it 'delivers an error webhook' do + expect { service_call }.to enqueue_job(SendWebhookJob) + .with( + 'customer.accounting_provider_error', + customer, + provider: 'netsuite', + provider_code: integration.code, + provider_error: { + message:, + error_code: code + } + ) + end + end + context 'when it is a client error' do let(:error_code) { 404 } let(:code) { 'invalid_secret_key_format' }