Method: PryExceptionExplorer.intercept

Defined in:
lib/pry-exception_explorer.rb

.intercept(*exceptions) {|lazy_frame, exception| ... } ⇒ Object

This method allows the user to assert the situations where an exception interception occurs. This method can be invoked in two ways. The general form takes a block, the block is passed both the frame where the exception was raised, and the exception itself. The user then creates an assertion (a stack-assertion) based on these attributes. If the assertion is later satisfied by a raised exception, that exception will be intercepted. In the second form, the method simply takes an exception class, or a number of exception classes. If one of these exceptions is raised, it will be intercepted.

Examples:

First form: Assert method name is toad and exception is an ArgumentError

PryExceptionExplorer.intercept do |frame, ex|
  frame.method_name == :toad && ex.is_a?(ArgumentError)
end

Second form: Assert exception is either ArgumentError or RuntimeError

PryExceptionExplorer.intercept(ArgumentError, RuntimeError)

Parameters:

  • The exception classes that will be intercepted.

Yields:

  • (lazy_frame, exception)

    The block that determines whether an exception will be intercepted.

Yield Parameters:

  • frame (PryExceptionExplorer::Lazyframe)

    The frame where the exception was raised.

  • exception (Exception)

    The exception that was raised.

Yield Returns:

  • (Boolean)

    The result of the stack assertion.



124
125
126
127
128
129
130
131
132
# File 'lib/pry-exception_explorer.rb', line 124

def intercept(*exceptions, &block)
  return if exceptions.empty? && block.nil?

  if !exceptions.empty?
    block = proc { |_, ex| exceptions.any? { |v| v === ex } }
  end

  local_hash[:intercept_object] = Intercept.new(block)
end