diff --git a/lib/quickbooks-ruby.rb b/lib/quickbooks-ruby.rb index 8ef4d42f..7187066b 100644 --- a/lib/quickbooks-ruby.rb +++ b/lib/quickbooks-ruby.rb @@ -273,7 +273,7 @@ class MissingRealmError < Error; end class UnsupportedOperation < Error; end class IntuitRequestException < Error - attr_accessor :message, :code, :detail, :type, :intuit_tid, :request_xml, :request_json + attr_accessor :message, :code, :detail, :element, :type, :intuit_tid, :request_xml, :request_json def initialize(msg) self.message = msg diff --git a/lib/quickbooks/service/base_service.rb b/lib/quickbooks/service/base_service.rb index bf691640..90a4dbc6 100644 --- a/lib/quickbooks/service/base_service.rb +++ b/lib/quickbooks/service/base_service.rb @@ -411,10 +411,12 @@ def response_body_messages(response) def parse_and_raise_exception(options = {}) err = parse_intuit_error - ex = Quickbooks::IntuitRequestException.new("#{err[:message]}:\n\t#{err[:detail]}") + element_msg = err[:element] ? "#{err[:element]}: " : "" + ex = Quickbooks::IntuitRequestException.new("#{element_msg}#{err[:message]}:\n\t#{err[:detail]}") ex.code = err[:code] ex.detail = err[:detail] ex.type = err[:type] + ex.element = err[:element] if err[:element] if is_json? ex.request_json = options[:request] else @@ -448,7 +450,7 @@ def parse_intuit_error end element_attr = error_element.attributes['element'] if element_attr - error[:element] = code_attr.value + error[:element] = element_attr.try(:value) end error[:message] = error_element.xpath("//xmlns:Message").text error[:detail] = error_element.xpath("//xmlns:Detail").text diff --git a/spec/fixtures/item_name_too_long_error.xml b/spec/fixtures/item_name_too_long_error.xml new file mode 100644 index 00000000..adc63937 --- /dev/null +++ b/spec/fixtures/item_name_too_long_error.xml @@ -0,0 +1,8 @@ + + + + String length is either shorter or longer than supported by specification + String length specified does not match the supported length. Min:0 Max:100 supported. Supplied length:103 + + + diff --git a/spec/lib/quickbooks/service/base_service_spec.rb b/spec/lib/quickbooks/service/base_service_spec.rb index 4f43c001..b78bbe44 100644 --- a/spec/lib/quickbooks/service/base_service_spec.rb +++ b/spec/lib/quickbooks/service/base_service_spec.rb @@ -58,6 +58,19 @@ expect { @service.send(:check_response, response) }.to raise_error(Quickbooks::IntuitRequestException) end + it "should parse all the error fields" do + xml = fixture('item_name_too_long_error.xml') + response = Struct.new(:code, :plain_body).new(400, xml) + expect { @service.send(:check_response, response, :request => xml) }.to raise_error { |error| + expect(error.type).to eq "ValidationFault" + expect(error.code).to eq "2050" + expect(error.element).to eq "Name" + expect(error.detail).to eq "String length specified does not match the supported length. Min:0 Max:100 supported. Supplied length:103" + expect(error.message).to include "String length is either shorter or longer than supported by specification" + expect(error.message).to include "String length specified does not match the supported length. Min:0 Max:100 supported. Supplied length:103" + } + end + it "should add request xml to request exception" do xml = fixture('generic_error.xml') xml2 = fixture('customer.xml')