class Eco::API::Session::Batch::Errors
Helper object linked to a `Batch::Status`. Its aim is to manage the errors of the batch status.
Constants
- ErrorCache
Attributes
@attr_reader status [Eco::API::Session::Batch::Status] `batch status` this `Errors` object is associated to.
Public Class Methods
@param status [Eco::API::Session::Batch::Status] `batch status` this `Errors` object is associated to.
# File lib/eco/api/session/batch/errors.rb, line 14 def initialize(status:) "Expected Batch::Status as root. Given: #{status.class}" unless status.is_a?(Eco::API::Session::Batch::Status) @status = status end
Public Instance Methods
Was there any Sever (reply) error as a result of this batch? @return [Boolean] `true` if any of the queried entries got an unsuccessful `Ecoportal::API::Common::BatchResponse`
# File lib/eco/api/session/batch/errors.rb, line 51 def any? queue.any? {|query| !status[query].success?} 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
# File lib/eco/api/session/batch/errors.rb, line 84 def by_type errors.group_by do |e| e.type end.transform_values do |arr| arr.map {|e| e.entry} end end
@return [Integer] the number of `entries` that got error.
# File lib/eco/api/session/batch/errors.rb, line 56 def count entries.length 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
# File lib/eco/api/session/batch/errors.rb, line 65 def errors entries.each_with_object([]) do |entry, arr| if body = status[entry].body if errs = body["errors"] errs.each do |msg| 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
# File lib/eco/api/session/batch/errors.rb, line 42 def logger status.logger end
@!group Messaging methods
# File lib/eco/api/session/batch/errors.rb, line 95 def message msgs = strs if msgs.length > 0 "There were #{msgs.length} errors:\n" + msgs.join("\n") else "There were no errors for the current batch '#{method}'!! ;)" end end
@see [Eco::API::Session::Batch::Status#method]
# File lib/eco/api/session/batch/errors.rb, line 27 def method status.method end
@!endgroup
# File lib/eco/api/session/batch/errors.rb, line 114 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
# File lib/eco/api/session/batch/errors.rb, line 104 def print msgs = strs if msgs.length > 0 logger.error("There were #{msgs.length} errors:\n" + msgs.join("\n")) else logger.info("There were no errors for the current batch '#{method}'!! ;)") end end
@see [Eco::API::Session::Batch::Status#queue]
# File lib/eco/api/session/batch/errors.rb, line 22 def queue status.queue end
@return [Eco::API::Session] currently active `session`
# File lib/eco/api/session/batch/errors.rb, line 38 def session status.session end
@param (see Eco::API::Session::Batch::Status#to_index
) @see [Eco::API::Session::Batch::Status#to_index]
# File lib/eco/api/session/batch/errors.rb, line 33 def to_index(*args) status.to_index(*args) end
Private Instance Methods
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>]
# File lib/eco/api/session/batch/errors.rb, line 126 def entries queue.select.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
# File lib/eco/api/session/batch/errors.rb, line 144 def get_attr(entry, attr) if entry.respond_to?(attr.to_sym) entry.public_send(attr.to_sym) elsif entry.is_a?(Hash) entry["#{attr}"] end end
# File lib/eco/api/session/batch/errors.rb, line 152 def get_row(value) case value when Eco::API::Common::People::PersonEntry value.idx when Ecoportal::API::V1::Person get_row(value.entry) end end
# File lib/eco/api/session/batch/errors.rb, line 184 def print_one(key) unless status.success?(key) logger.error(str(key)) end end
Generates a `String` specifying the error for the entry `key`. @param key [Integer, Ecoportal::API::V1::Person] @return [String] the error description.
# File lib/eco/api/session/batch/errors.rb, line 172 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.
# File lib/eco/api/session/batch/errors.rb, line 163 def strs by_type.values.flatten(1).each_with_object([]) do |query, msgs| msgs.push(str(query)) end end