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/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 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