lib/eco/api/session/batch/errors.rb in eco-helpers-2.0.5 vs lib/eco/api/session/batch/errors.rb in eco-helpers-2.0.6

- old
+ new

@@ -6,10 +6,12 @@ class Errors # @attr_reader status [Eco::API::Session::Batch::Status] `batch status` this `Errors` object is associated to. attr_reader :status + ErrorCache = Struct.new(:type, :err, :entry) + # @param status [Eco::API::Session::Batch::Status] `batch status` this `Errors` object is associated to. def initialize(status:) "Expected Batch::Status as root. Given: #{status.class}" unless status.is_a?(Eco::API::Session::Batch::Status) @status = status end @@ -53,97 +55,45 @@ # @return [Integer] the number of `entries` that got error. def count entries.length end - # Input entries that got **error** response from the _Server_. - # @raise [Exception] if there are elements of the final `queue` that did not get response - # @note discards those that did not get _response_ from the Server (so those that were not queried) - # - please, observe that this can only happen if there were repeated entries in the `source_queue` - # @return [Array<Hash>, Array<Ecoportal::API::V1::Person>, Array<Ecoportal::API::Internal::Person>] - def entries - queue.each_with_index.map do |query, i| - unless response = status[i] - msg = "Error: query with no response. You might have duplicated entries in your queue.\n" - msg += "Queue length: #{queue.length}; Queue elements class: #{queue.first.class}\n" - msg += "Query with no response. Person: #{person_ref(query)}\n" - queue.map do |entry| - if [:id, :external_id, :email].any? {|attr| (v = get_attr(entry, attr)) && v == get_attr(query, attr)} - msg += "It could be this peson entry (idx: #{to_index(entry)}): #{person_ref(entry)}\n" - end - end - raise msg - end - - response.success?? nil : query - end.compact - end - # For all the `entries` with errors generates a `Hash` object # @return [Array<Hash>] where each `Hash` has # 1. `:type` -> the error type # 2. `:err` -> the error `class` of that `:type` # 3. `:entry` -> the entry that generated the error def errors entries.each_with_object([]) do |entry, arr| if body = status[entry].body if errs = body["errors"] errs.each do |msg| - arr.push({ - type: klass = Eco::API::Error.get_type(msg), - err: klass.new(err_msg: msg, entry: entry, session: session), - entry: entry - }) + arr.push(ErrorCache.new( + klass = Eco::API::Error.get_type(msg), + klass.new(err_msg: msg, entry: entry, session: session), + entry + )) end end end end end # Groups `entries` with error `type` # @return [Hash] where each `key` is a `type` **error** and each value is # an `Array` of `entries` that got that error def by_type - errors.group_by do |h| - h[:type] + errors.group_by do |e| + e.type end.transform_values do |arr| - arr.map {|h| h[:entry]} + arr.map {|e| e.entry} end end # @!endgroup # @!group Messaging methods - # Generates a `String` specifying the error for the entry `key`. - # @param key [Integer, Ecoportal::API::V1::Person] - # @return [String] the error description. - def str(key) - msg = "" - unless status.success?(key) - i = to_index(key) - entry = queue.to_a[i] - response = status[i] - msg = "Error #{response.status}: #{response.body}\n" - msg += "-- Failed to batch #{method}. Person: #{person_ref(entry)}" - end - msg - end - - # Sorts the entries that got server error by error `type` and generates the error messages. - # @return [Array<String>] the errors messages. - def strs - by_type.values.flatten(1).each_with_object([]) do |query, msgs| - msgs.push(str(query)) - end - end - - def print_one(key) - unless status.success?(key) - logger.error(str(key)) - end - end - def message msgs = strs if msgs.length > 0 "There were #{msgs.length} errors:\n" + msgs.join("\n") else @@ -161,10 +111,33 @@ end # @!endgroup private + # Input entries that got **error** response from the _Server_. + # @raise [Exception] if there are elements of the final `queue` that did not get response + # @note discards those that did not get _response_ from the Server (so those that were not queried) + # - please, observe that this can only happen if there were repeated entries in the `source_queue` + # @return [Array<Hash>, Array<Ecoportal::API::V1::Person>, Array<Ecoportal::API::Internal::Person>] + def entries + queue.filter.with_index do |query, i| + unless response = status[i] + msg = "Error: query with no response. You might have duplicated entries in your queue.\n" + msg += "Queue length: #{queue.length}; Queue elements class: #{queue.first.class}\n" + msg += "Query with no response. Person: #{person_ref(query)}\n" + queue.map do |entry| + if [:id, :external_id, :email].any? {|attr| (v = get_attr(entry, attr)) && v == get_attr(query, attr)} + msg += "It could be this peson entry (idx: #{to_index(entry)}): #{person_ref(entry)}\n" + end + end + raise msg + end + + !response.success? + end + end + def person_ref(entry) row_str = (row = get_row(entry)) ? "(row: #{row}) " : nil "#{row_str}(id: '#{get_attr(entry, :id)}') '#{get_attr(entry, :name)}' ('#{get_attr(entry, :external_id)}': '#{get_attr(entry, :email)}')" end @@ -180,9 +153,38 @@ case value when Eco::API::Common::People::PersonEntry value.idx when Ecoportal::API::V1::Person get_row(value.entry) + end + end + + # Sorts the entries that got server error by error `type` and generates the error messages. + # @return [Array<String>] the errors messages. + def strs + by_type.values.flatten(1).each_with_object([]) do |query, msgs| + msgs.push(str(query)) + end + end + + # Generates a `String` specifying the error for the entry `key`. + # @param key [Integer, Ecoportal::API::V1::Person] + # @return [String] the error description. + def str(key) + msg = "" + unless status.success?(key) + i = to_index(key) + entry = queue.to_a[i] + response = status[i] + msg = "Error #{response.status}: #{response.body}\n" + msg += "-- Failed to batch #{method}. Person: #{person_ref(entry)}" + end + msg + end + + def print_one(key) + unless status.success?(key) + logger.error(str(key)) end end end end