Skip to content

Commit

Permalink
misc(netsuite): Process payload errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad committed Aug 27, 2024
1 parent 75aeec2 commit eb19bd7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/services/integrations/aggregator/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"error": {
"code": "action_script_runtime_error",
"payload": {
"name": "TypeError",
"message": "Please enter value(s) for: Company Name"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down

0 comments on commit eb19bd7

Please sign in to comment.