lib/eco/api/session/batch/errors.rb in eco-helpers-2.0.41 vs lib/eco/api/session/batch/errors.rb in eco-helpers-2.0.42
- old
+ new
@@ -6,11 +6,11 @@
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)
+ ErrorCache = Struct.new(:type, :err, :entry, :response)
# @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
@@ -44,54 +44,63 @@
end
# @!endgroup
# @!group Pure errors helper methods
+ # @return [Integer] the number of `entries` that got error.
+ def count
+ entries.length
+ end
+
# 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`
def any?
queue.any? {|query| !status[query].success?}
end
- # @return [Integer] the number of `entries` that got error.
- def count
- entries.length
+ # Groups `entries` with error `type`
+ # @return [Hash] where each `key` is a `type` **error** and each value is an `Array` of:
+ # 1. `entries` that got that error, if `only_entries` is `true`
+ # 2. `ErrorCache` objects, if `only_entries` is `false`
+ def by_type(only_entries: true)
+ errors.group_by do |e|
+ e.type
+ end.transform_values do |arr|
+ if only_entries
+ arr.map {|e| e.entry}
+ else
+ arr
+ end
+ end
end
+ # @!endgroup
- # 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
+ # For all the `entries` with errors generates an `Array` of `ErrorCache` objects
+ # @return [Array<Eco::API::Session::Batch::Errors::ErrorCache>] where each `object` has
+ # 1. `type` -> the error type `Class`
+ # 2. `err` -> an instance object of that error `class` type
+ # 3. `entry` -> the entry that generated the error
+ # 4. `response` -> the original response from the server that carries the error
def errors
entries.each_with_object([]) do |entry, arr|
- if body = status[entry].body
- if errs = body["errors"]
+ response = status[entry]
+ if body = response.body
+ if errs = body["errors"] || body["error"]
+ errs = [errs].flatten(1).compact
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
+ entry,
+ response
))
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 |e|
- e.type
- end.transform_values do |arr|
- arr.map {|e| e.entry}
- end
- end
- # @!endgroup
-
# @!group Messaging methods
def message
msgs = strs
if msgs.length > 0
@@ -110,12 +119,11 @@
end
end
# @!endgroup
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)}')"
+ Eco::API::Session::Batch::Feedback.person_ref(entry)
end
private
# Input entries that got **error** response from the _Server_.
@@ -140,23 +148,14 @@
!response.success?
end
end
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
+ Eco::API::Session::Batch::Feedback.get_attr(entry, attr)
end
def get_row(value)
- case value
- when Eco::API::Common::People::PersonEntry
- value.idx
- when Ecoportal::API::V1::Person
- get_row(value.entry)
- end
+ Eco::API::Session::Batch::Feedback.get_row(value)
end
# Sorts the entries that got server error by error `type` and generates the error messages.
# @return [Array<String>] the errors messages.
def strs