Exception: Webhookdb::AggregateResult

Inherits:
WebhookdbError show all
Defined in:
lib/webhookdb/aggregate_result.rb

Overview

Value returned or raised from a method that deals with aggregate success or failure.

Sometimes we want to process a collection of items, and not fail the entire thing if some fail. In this case, we can return an AggregateResult. If the AggregateResult is returned, we know all items processed. If it is raised, at least some of the items errored.

Example:

ag = AggregateResult.new
items.each do |item|
  ag.success(myfunc(item))
rescue => e
  ag.failure(item, e)
end
return ag.finish

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(existing = nil) ⇒ AggregateResult

Returns a new instance of AggregateResult.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/webhookdb/aggregate_result.rb', line 24

def initialize(existing=nil)
  if existing.nil?
    @successes = []
    @failures = []
    @errors = []
    super("awaiting result")
    return
  end
  # We can only set the exception message from initialization
  @successes = existing.successes
  @failures = existing.failures
  @errors = existing.errors
  if @failures.empty?
    super("No errors")
    return
  end

  lines = ["Multiple errors occurred:"]
  @failures.each_with_index do |f, i|
    lines << " #{f.inspect}: #{@errors[i].message}"
  end
  super(lines.join("\n"))
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



22
23
24
# File 'lib/webhookdb/aggregate_result.rb', line 22

def errors
  @errors
end

#failuresObject (readonly)

Returns the value of attribute failures.



22
23
24
# File 'lib/webhookdb/aggregate_result.rb', line 22

def failures
  @failures
end

#successesObject (readonly)

Returns the value of attribute successes.



22
23
24
# File 'lib/webhookdb/aggregate_result.rb', line 22

def successes
  @successes
end

Instance Method Details

#failure(i, e) ⇒ Object



52
53
54
55
# File 'lib/webhookdb/aggregate_result.rb', line 52

def failure(i, e)
  @failures << i
  @errors << e
end

#finishObject



57
58
59
60
61
62
# File 'lib/webhookdb/aggregate_result.rb', line 57

def finish
  raise InvalidPrecondition, "failures.length must equal errors.length" unless @failures.length == @errors.length
  result = Webhookdb::AggregateResult.new(self)
  return result if self.failures.empty?
  raise result
end

#success(i) ⇒ Object



48
49
50
# File 'lib/webhookdb/aggregate_result.rb', line 48

def success(i)
  @successes << i
end