Class: Eco::API::Session::Batch::Errors
- Inherits:
-
Object
- Object
- Eco::API::Session::Batch::Errors
- Defined in:
- lib/eco/api/session/batch/errors.rb
Overview
Helper object linked to a Batch::Status
. Its aim is to manage the errors of the batch status.
Instance Attribute Summary collapse
- #status ⇒ Object readonly
Status object shortcuts collapse
- #logger ⇒ Object
- #method ⇒ Object
- #queue ⇒ Object
-
#session ⇒ Eco::API::Session
Currently active
session
. - #to_index(*args) ⇒ Object
Pure errors helper methods collapse
-
#any? ⇒ Boolean
Was there any Sever (reply) error as a result of this batch?.
-
#by_type ⇒ Hash
Groups
entries
with errortype
. -
#count ⇒ Integer
The number of
entries
that got error. -
#entries ⇒ Array<Hash>, ...
Input entries that got error response from the Server.
-
#errors ⇒ Array<Hash>
For all the
entries
with errors generates aHash
object.
Messaging methods collapse
- #message ⇒ Object
- #print ⇒ Object
- #print_one(key) ⇒ Object
-
#str(key) ⇒ String
Generates a
String
specifying the error for the entrykey
. -
#strs ⇒ Array<String>
Sorts the entries that got server error by error
type
and generates the error messages.
Instance Method Summary collapse
-
#initialize(status:) ⇒ Errors
constructor
A new instance of Errors.
Constructor Details
Instance Attribute Details
#status ⇒ Object (readonly)
9 10 11 |
# File 'lib/eco/api/session/batch/errors.rb', line 9 def status @status end |
Instance Method Details
#any? ⇒ Boolean
Was there any Sever (reply) error as a result of this batch?
49 50 51 |
# File 'lib/eco/api/session/batch/errors.rb', line 49 def any? queue.any? {|query| !status[query].success?} end |
#by_type ⇒ Hash
Groups entries
with error type
105 106 107 108 109 110 111 |
# File 'lib/eco/api/session/batch/errors.rb', line 105 def by_type errors.group_by do |h| h[:type] end.transform_values do |arr| arr.map {|h| h[:entry]} end end |
#count ⇒ Integer
Returns the number of entries
that got error.
54 55 56 |
# File 'lib/eco/api/session/batch/errors.rb', line 54 def count entries.length end |
#entries ⇒ Array<Hash>, ...
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
Input entries that got error response from the Server.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/eco/api/session/batch/errors.rb', line 63 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 |
#errors ⇒ Array<Hash>
For all the entries
with errors generates a Hash
object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/eco/api/session/batch/errors.rb', line 86 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 }) end end end end end |
#logger ⇒ Object
40 41 42 |
# File 'lib/eco/api/session/batch/errors.rb', line 40 def logger status.logger end |
#message ⇒ Object
145 146 147 148 149 150 151 152 |
# File 'lib/eco/api/session/batch/errors.rb', line 145 def 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 |
#method ⇒ Object
25 26 27 |
# File 'lib/eco/api/session/batch/errors.rb', line 25 def method status.method end |
#print ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/eco/api/session/batch/errors.rb', line 154 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 |
#print_one(key) ⇒ Object
139 140 141 142 143 |
# File 'lib/eco/api/session/batch/errors.rb', line 139 def print_one(key) unless status.success?(key) logger.error(str(key)) end end |
#queue ⇒ Object
20 21 22 |
# File 'lib/eco/api/session/batch/errors.rb', line 20 def queue status.queue end |
#session ⇒ Eco::API::Session
Returns currently active session
.
36 37 38 |
# File 'lib/eco/api/session/batch/errors.rb', line 36 def session status.session end |
#str(key) ⇒ String
Generates a String
specifying the error for the entry key
.
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/eco/api/session/batch/errors.rb', line 119 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} (entry #{i+1}). Person: #{person_ref(entry)}" end msg end |
#strs ⇒ Array<String>
Sorts the entries that got server error by error type
and generates the error messages.
133 134 135 136 137 |
# File 'lib/eco/api/session/batch/errors.rb', line 133 def strs by_type.values.flatten(1).each_with_object([]) do |query, msgs| msgs.push(str(query)) end end |
#to_index(*args) ⇒ Object
31 32 33 |
# File 'lib/eco/api/session/batch/errors.rb', line 31 def to_index(*args) status.to_index(*args) end |