collectiveidea / interactor

Interactor provides a common interface for performing complex user interactions.
MIT License
3.36k stars 212 forks source link

Accidental rescuing Interactor::Failure errors in an interactor. #120

Closed hedgesky closed 7 years ago

hedgesky commented 7 years ago

Currently context.fail! raises an error but there is no mention about that in Readme. Therefore a user could accidentally rescue these errors. Example:

class A
  include Interactor

  def call
    do_some_stuff
  rescue => e
    logger.error("#{e.class}: #{e.message}")
  end

  private

  def do_some_stuff
    context.fail!
  end
end

Here user tries to log unforeseen errors but instead finds such lines:

Interactor::Failure: #<Interactor::Context>

From my point of view, we should at least add information about that in README with possible solution:

def call
  do_some_stuff
rescue Interactor::Failure
  raise
rescue => e
  puts "#{e.class}: #{e.message}"
end

But ideal solution is using throw instead of raise. It would stop execution (like raise does) and wouldn't require additional rescues. I can make a PR if you find this solution is acceptable.

laserlemon commented 7 years ago

Version 4.0 will use throw and catch rather than raise and rescue for this very reason. My attention is turning to development of 4.0, but I would be happy to merge a readme update with a brief mention of context.fail!'s internal behavior and a warning that using a blanket rescue in your interactor may swallow the failure.