Class: CMDx::Fault

Inherits:
Error
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cmdx/faults.rb

Overview

Base fault class for handling task execution failures and interruptions.

Faults represent error conditions that occur during task execution, providing a structured way to handle and categorize different types of failures. Each fault contains a reference to the result object that caused the fault.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ Fault

Initialize a new fault with the given result.

Examples:

fault = Fault.new(task_result)
fault.result.reason # => "Task validation failed"

Parameters:

  • result (Result)

    the result object that caused this fault

Raises:

  • (ArgumentError)

    if result is nil or invalid



37
38
39
40
41
# File 'lib/cmdx/faults.rb', line 37

def initialize(result)
  @result = result

  super(result.reason)
end

Instance Attribute Details

#resultResult (readonly)

Returns the result that caused this fault.

Examples:

fault.result.reason # => "Validation failed"

Returns:

  • (Result)

    The result instance



22
23
24
# File 'lib/cmdx/faults.rb', line 22

def result
  @result
end

Class Method Details

.for?(*tasks) ⇒ Class

Create a fault class that matches specific task types.

Examples:

Fault.for?(UserTask, AdminUserTask)
# => true if fault.task is a UserTask or AdminUserTask

Parameters:

  • tasks (Array<Class>)

    array of task classes to match against

Returns:

  • (Class)

    a new fault class that matches the specified tasks



56
57
58
59
60
61
62
63
64
# File 'lib/cmdx/faults.rb', line 56

def for?(*tasks)
  temp_fault = Class.new(self) do
    def self.===(other)
      other.is_a?(superclass) && @tasks.any? { |task| other.task.is_a?(task) }
    end
  end

  temp_fault.tap { |c| c.instance_variable_set(:@tasks, tasks) }
end

.matches?(&block) ⇒ Class

Create a fault class that matches based on a custom block.

Examples:

Fault.matches? { |fault| fault.result.[:critical] }
# => true if fault has critical metadata

Parameters:

  • block (Proc)

    block that determines if a fault matches

Returns:

  • (Class)

    a new fault class that matches based on the block

Raises:

  • (ArgumentError)

    if no block is provided



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cmdx/faults.rb', line 79

def matches?(&block)
  raise ArgumentError, "block required" unless block_given?

  temp_fault = Class.new(self) do
    def self.===(other)
      other.is_a?(superclass) && @block.call(other)
    end
  end

  temp_fault.tap { |c| c.instance_variable_set(:@block, block) }
end