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.

Defined Under Namespace

Classes: ErrorCache

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:



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

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

  @status = status
end

Instance Attribute Details

#statusObject (readonly)



8
9
10
# File 'lib/eco/api/session/batch/errors.rb', line 8

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



58
59
60
# File 'lib/eco/api/session/batch/errors.rb', line 58

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

#by_type(only_entries: true) ⇒ Hash

Groups entries with error type

Returns:

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


66
67
68
69
70
71
72
# File 'lib/eco/api/session/batch/errors.rb', line 66

def by_type(only_entries: true)
  errors.group_by(&:type).transform_values do |arr|
    next arr unless only_entries

    arr.map(&:entry)
  end
end

#countInteger

Returns the number of entries that got error.

Returns:

  • (Integer)

    the number of entries that got error.



51
52
53
# File 'lib/eco/api/session/batch/errors.rb', line 51

def count
  entries.length
end

#errorsArray<Eco::API::Session::Batch::Errors::ErrorCache>

For all the entries with errors generates an Array of ErrorCache objects

Returns:

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


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/eco/api/session/batch/errors.rb', line 81

def errors
  entries.each_with_object([]) do |entry, arr|
    response = status[entry]
    next unless (body = response.body)

    errs = []
    case body
    when Hash
      body_errs = body["errors"] || body["error"]
      errs = [body_errs].flatten(1).compact if body_errs

      response_errs = errs.empty? && !response.success?
      errs = [body["response"]].flatten(1).compact if response_errs
    when String
      errs = [body]
    end

    errs.each do |msg|
      err_cache = ErrorCache.new(
        klass = Eco::API::Error.get_type(msg),
        klass.new(err_msg: msg, entry: entry, session: session),
        entry,
        response
      )
      arr.push(err_cache)
    end
  end
end

#loggerObject



43
44
45
# File 'lib/eco/api/session/batch/errors.rb', line 43

def logger
  status.logger
end

#messageObject



112
113
114
115
116
117
118
119
# File 'lib/eco/api/session/batch/errors.rb', line 112

def message
  msgs = strs
  if msgs.empty?
    "There were no errors for the current batch '#{method}'!! ;)"
  else
    "There were #{msgs.length} errors:\n" + msgs.join("\n")
  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]


28
29
30
# File 'lib/eco/api/session/batch/errors.rb', line 28

def method
  status.method
end

#person_ref(entry) ⇒ Object



131
132
133
# File 'lib/eco/api/session/batch/errors.rb', line 131

def person_ref(entry)
  Eco::API::Session::Batch::Feedback.person_ref(entry)
end


121
122
123
124
125
126
127
128
# File 'lib/eco/api/session/batch/errors.rb', line 121

def print
  msgs = strs
  if msgs.empty?
    logger.info("There were no errors for the current batch '#{method}'!! ;)")
  else
    logger.error("There were #{msgs.length} errors:\n" + msgs.join("\n"))
  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]


23
24
25
# File 'lib/eco/api/session/batch/errors.rb', line 23

def queue
  status.queue
end

#sessionEco::API::Session

Returns currently active session.

Returns:



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

def session
  status.session
end

#to_index(*args) ⇒ Object

Parameters:

  • key (Integer, Hash, Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person)

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]


34
35
36
# File 'lib/eco/api/session/batch/errors.rb', line 34

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