Class: Eco::API::Session::Batch::Errors

Inherits:
Object
  • Object
show all
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 shortcuts collapse

Pure errors helper methods collapse

Messaging methods collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:) ⇒ Errors

Returns a new instance of Errors.

Parameters:



12
13
14
15
# File 'lib/eco/api/session/batch/errors.rb', line 12

def initialize(status:)
  "Expected Batch::Status as root. Given: #{status.class}" unless status.is_a?(Eco::API::Session::Batch::Status)
  @status = status
end

Instance Attribute Details

#statusObject (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?

Returns:

  • (Boolean)

    true if any of the queried entries got an unsuccessful Ecoportal::API::Common::BatchResponse



49
50
51
# File 'lib/eco/api/session/batch/errors.rb', line 49

def any?
  queue.any? {|query| !status[query].success?}
end

#by_typeHash

Groups entries with error type

Returns:

  • (Hash)

    where each key is a type error and each value is an Array of entries that got that error



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

#countInteger

Returns the number of entries that got error.

Returns:

  • (Integer)

    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

#entriesArray<Hash>, ...

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

Input entries that got error response from the Server.

Returns:

Raises:

  • (Exception)

    if there are elements of the final queue that did not get response



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

#errorsArray<Hash>

For all the entries with errors generates a Hash object

Returns:

  • (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


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

#loggerObject



40
41
42
# File 'lib/eco/api/session/batch/errors.rb', line 40

def logger
  status.logger
end

#messageObject



145
146
147
148
149
150
151
152
# File 'lib/eco/api/session/batch/errors.rb', line 145

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

#methodObject

See Also:

  • Eco::API::Session::Batch::Errors.[Eco[Eco::API[Eco::API::Session[Eco::API::Session::Batch[Eco::API::Session::Batch::Status[Eco::API::Session::Batch::Status#method]


25
26
27
# File 'lib/eco/api/session/batch/errors.rb', line 25

def method
  status.method
end


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


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

#queueObject

See Also:

  • Eco::API::Session::Batch::Errors.[Eco[Eco::API[Eco::API::Session[Eco::API::Session::Batch[Eco::API::Session::Batch::Status[Eco::API::Session::Batch::Status#queue]


20
21
22
# File 'lib/eco/api/session/batch/errors.rb', line 20

def queue
  status.queue
end

#sessionEco::API::Session

Returns currently active session.

Returns:



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.

Parameters:

Returns:

  • (String)

    the error description.



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

#strsArray<String>

Sorts the entries that got server error by error type and generates the error messages.

Returns:

  • (Array<String>)

    the errors 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

Parameters:

See Also:

  • Eco::API::Session::Batch::Errors.[Eco[Eco::API[Eco::API::Session[Eco::API::Session::Batch[Eco::API::Session::Batch::Status[Eco::API::Session::Batch::Status#to_index]


31
32
33
# File 'lib/eco/api/session/batch/errors.rb', line 31

def to_index(*args)
  status.to_index(*args)
end