Exception: Webhookdb::AggregateResult
- Inherits:
-
WebhookdbError
- Object
- StandardError
- WebhookdbError
- Webhookdb::AggregateResult
- 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
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#failures ⇒ Object
readonly
Returns the value of attribute failures.
-
#successes ⇒ Object
readonly
Returns the value of attribute successes.
Instance Method Summary collapse
- #failure(i, e) ⇒ Object
- #finish ⇒ Object
-
#initialize(existing = nil) ⇒ AggregateResult
constructor
A new instance of AggregateResult.
- #success(i) ⇒ Object
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].}" end super(lines.join("\n")) end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
22 23 24 |
# File 'lib/webhookdb/aggregate_result.rb', line 22 def errors @errors end |
#failures ⇒ Object (readonly)
Returns the value of attribute failures.
22 23 24 |
# File 'lib/webhookdb/aggregate_result.rb', line 22 def failures @failures end |
#successes ⇒ Object (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 |
#finish ⇒ Object
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 |