Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Automatically fail context on exception #176

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

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