lib/zuora/response.rb in zuora-ruby-0.3.0 vs lib/zuora/response.rb in zuora-ruby-0.4.0
- old
+ new
@@ -1,9 +1,10 @@
require 'active_support/core_ext/hash/conversions'
module Zuora
class Response
+ ERROR_STRINGS = ['Missing required value', 'are required fields'].freeze
attr_reader :raw
# @param [Faraday::Response]
# @return [Zuora::Response]
def initialize(response)
@@ -16,12 +17,36 @@
doc = Nokogiri::XML raw.body
hash = Hash.from_xml doc.to_xml
Hashie::Mash.new(symbolize_keys_deep(hash))
end
+ # @param [Hash] hash
+ def handle_errors(hash)
+ errors = []
+
+ hash.each do |key, value|
+ if value.is_a?(Hash)
+ handle_errors(value)
+ elsif value.is_a?(Array)
+ value.each { |v| handle_errors(v) }
+ elsif error?(key, value)
+ errors << value
+ end
+ end
+
+ raise_errors(errors) if errors.present?
+ end
+
private
+ # @param [String|Symbol] key
+ # @param [String] value
+ def error?(key, value)
+ ERROR_STRINGS.any? { |str| value.to_s.match(str) } ||
+ key.to_s.casecmp('message').zero?
+ end
+
# Given a key, convert to symbol, removing XML namespace, if any.
# @param [String] key - a key, either "abc" or "abc:def"
# @return [Symbol]
def symbolize_key(key)
return key unless key.respond_to?(:split)
@@ -43,8 +68,15 @@
value = symbolize_keys_deep(value)
end
[symbolize_key(key), value]
end]
+ end
+
+ # @param [Array] errors
+ def raise_errors(errors)
+ error_string = errors.join(',')
+ error = Zuora::Errors::InvalidValue.new(error_string, to_h)
+ fail error
end
end
end