Module: Taski::AggregateAware

Included in:
TaskError
Defined in:
lib/taski.rb

Overview

Note:

TaskError and all TaskClass::Error classes already extend this module.

Mixin for exception classes to enable transparent rescue matching with AggregateError.

How it works

Ruby's rescue clause uses the === operator to match exceptions. This module overrides === so that when the rescued class is compared against an AggregateError, it checks whether the AggregateError contains an error of that type. This means rescue TaskError will match an AggregateError that wraps one or more TaskError instances, even though AggregateError does not inherit from TaskError.

Why this exists

Taski's Executor always raises AggregateError (even for a single failure) to provide a uniform error interface. Without AggregateAware, callers would always need to rescue AggregateError and inspect its contents. With this module, callers can rescue the specific error class they care about:

rescue MyTask::Error => e  # matches AggregateError containing MyTask::Error
rescue Taski::TaskError    # matches AggregateError containing any TaskError

Examples:

begin
  MyTask.value  # raises AggregateError containing MyTask::Error
rescue MyTask::Error => e
  puts "MyTask failed: #{e.message}"
end

Instance Method Summary collapse

Instance Method Details

#===(other) ⇒ Object



84
85
86
87
88
# File 'lib/taski.rb', line 84

def ===(other)
  return super unless other.is_a?(Taski::AggregateError)

  other.includes?(self)
end