From 079154e6226fd6abd395d52c4b361baee373cff0 Mon Sep 17 00:00:00 2001 From: Daniel Beban Date: Mon, 22 Jan 2024 16:01:45 +0000 Subject: [PATCH 1/2] Fix to address failing test for journal_entry The :upsert_list action should be correctly formatted using snake case since the action is defined as NetSuite::Actions::UpsertList. --- lib/netsuite/records/journal_entry.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/netsuite/records/journal_entry.rb b/lib/netsuite/records/journal_entry.rb index 59391bf52..369460164 100644 --- a/lib/netsuite/records/journal_entry.rb +++ b/lib/netsuite/records/journal_entry.rb @@ -7,7 +7,7 @@ class JournalEntry include Support::Actions include Namespaces::TranGeneral - actions :get, :get_list, :add, :delete, :search, :upsert, :upsertlist + actions :get, :get_list, :add, :delete, :search, :upsert, :upsert_list fields :approved, :created_date, :exchange_rate, :last_modified_date, :memo, :reversal_date, :reversal_defer, :reversal_entry, :tran_date, :tran_id From 4012126bf60c2a8d512cc25c67a1b7515676eadb Mon Sep 17 00:00:00 2001 From: Daniel Beban Date: Mon, 22 Jan 2024 14:58:21 +0000 Subject: [PATCH 2/2] Raise error message from SOAP response when initialize action fails The current error handling approach of the initalize action is to raise a generic failure message. For example: NetSuite::Records::ItemReceipt.initialize with # failed. This does not provide any of the useful context from the SOAP response which would be more helpful for debugging the issue. Support for fetching error context from the SOAP response XML has been added to other actions, for example: * https://github.com/netsweet/netsuite/commit/3b756b9d7b802375ffb9d059b303dc2e18e94d84 * https://github.com/netsweet/netsuite/commit/e6401961ef35e8d60b30382c810e893ade2e0e44 * https://github.com/netsweet/netsuite/commit/70ab07710387ce32f746a6213ec1087a0f764ae7 Adding similar support to the initialize action, and using the message of the first error in the SOAP XML for the InitializationError raise, would provide more useful error context for debugging. --- lib/netsuite/actions/initialize.rb | 18 +++++++++++++++--- spec/netsuite/records/vendor_bill_spec.rb | 8 +++++--- spec/netsuite/records/vendor_payment_spec.rb | 8 +++++--- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/netsuite/actions/initialize.rb b/lib/netsuite/actions/initialize.rb index 24ca62dca..da10d46fc 100644 --- a/lib/netsuite/actions/initialize.rb +++ b/lib/netsuite/actions/initialize.rb @@ -56,6 +56,20 @@ def action_name :initialize end + def response_errors + if response_hash[:status] && response_hash[:status][:status_detail] + @response_errors ||= errors + end + end + + def errors + error_obj = response_hash[:status][:status_detail] + error_obj = [error_obj] if error_obj.instance_of?(Hash) + error_obj.map do |error| + NetSuite::Error.new(error) + end + end + module Support def self.included(base) @@ -74,13 +88,11 @@ def initialize(object, credentials={}) if response.success? new(response.body) else - raise InitializationError, "#{self}.initialize with #{object} failed." + raise InitializationError, response.errors.find { |e| e.type == 'ERROR' }.message end end - end end - end end end diff --git a/spec/netsuite/records/vendor_bill_spec.rb b/spec/netsuite/records/vendor_bill_spec.rb index 7c76a3690..5b225c5db 100644 --- a/spec/netsuite/records/vendor_bill_spec.rb +++ b/spec/netsuite/records/vendor_bill_spec.rb @@ -138,14 +138,16 @@ end context 'when the response is unsuccessful' do - let(:response) { NetSuite::Response.new(:success => false, :body => {}) } + let(:error1) { NetSuite::Error.new(:@type => 'ERROR', :message => 'You can not initialize vendorbill: invalid reference.') } + let(:error2) { NetSuite::Error.new(:@type => 'ERROR', :message => 'some message') } + let(:response) { NetSuite::Response.new(:success => false, :body => {}, :errors => [error1, error2]) } - it 'raises a InitializationError exception' do + it 'raises a InitializationError exception capturing the message of the first error in the response object' do expect(NetSuite::Actions::Initialize).to receive(:call).with([NetSuite::Records::VendorBill, vendor], {}).and_return(response) expect { NetSuite::Records::VendorBill.initialize(vendor) }.to raise_error(NetSuite::InitializationError, - /NetSuite::Records::VendorBill.initialize with .+ failed./) + /You can not initialize vendorbill: invalid reference/) end end end diff --git a/spec/netsuite/records/vendor_payment_spec.rb b/spec/netsuite/records/vendor_payment_spec.rb index 2f8d28cb6..f680ad796 100644 --- a/spec/netsuite/records/vendor_payment_spec.rb +++ b/spec/netsuite/records/vendor_payment_spec.rb @@ -95,14 +95,16 @@ end context 'when the response is unsuccessful' do - let(:response) { NetSuite::Response.new(:success => false, :body => {}) } + let(:error1) { NetSuite::Error.new(:@type => 'ERROR', :message => 'You can not initialize vendorpayment: invalid reference.') } + let(:error2) { NetSuite::Error.new(:@type => 'ERROR', :message => 'some message') } + let(:response) { NetSuite::Response.new(:success => false, :body => {}, :errors => [error1, error2]) } - it 'raises a InitializationError exception' do + it 'raises a InitializationError exception capturing the message of the first error in the response object' do expect(NetSuite::Actions::Initialize).to receive(:call).with([NetSuite::Records::VendorPayment, vendor], {}).and_return(response) expect { NetSuite::Records::VendorPayment.initialize(vendor) }.to raise_error(NetSuite::InitializationError, - /NetSuite::Records::VendorPayment.initialize with .+ failed./) + /You can not initialize vendorpayment: invalid reference/) end end end