Skip to content

Conversation

@matas-zanevicius
Copy link

What's the reason for this feature?

Sometimes we may have a situation where our interactors should fail in case there is an exception raised. Currently, we have to rescue exceptions in order to fail the context. consider the following example:

class MyInteractor
  include Interactor

  def call
    method_raising_an_exception
  rescue NameError => e
    ExceptionLogger.log(e)
    context.fail!(error: e)
  end
end

This example above is simple, but it may get more complex when you have to rescue multiple different exceptions in the same interactor class in different places. However, what you actually want is to fail the context on those exceptions. And perhaps log them, so that they don't halt your program, but don't go unnoticed either.

My proposal: define what kinds of exceptions must be rescued, and provide a proc, if necessary, with custom behaviour for rescued exception(s) handling.

class MyInteractor
  include Interactor

  fail_on_exeption NameError, exception_handler: ->(e) { ExceptionLogger.log(e) }

  def call
    method_raising_an_exception
  end
end 

NameError is silly, I only use it to make my example obvious. In real life, you would probably rescue from some other errors or your own custom error...

@timlawrenz
Copy link

@matas-zanevicius this looks awesome. Would you mind looking at the merge conflict?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants