Skip to content

Commit

Permalink
Raise error message from SOAP response when initialize action fails
Browse files Browse the repository at this point in the history
The current error handling approach of the initalize action is to raise
a generic failure message.

For example:
NetSuite::Records::ItemReceipt.initialize with #<NetSuite::Records::PurchaseOrder:0x00007f561d8717c8> 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:
* 3b756b9
* e640196
* 70ab077

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.
  • Loading branch information
Daniel Beban committed Jan 22, 2024
1 parent 079154e commit 4012126
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
18 changes: 15 additions & 3 deletions lib/netsuite/actions/initialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
8 changes: 5 additions & 3 deletions spec/netsuite/records/vendor_bill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions spec/netsuite/records/vendor_payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4012126

Please sign in to comment.