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

How about an "after" hook that will be called after "fail_and_return!"? #242

Open
vittorius opened this issue Jan 24, 2023 · 2 comments
Open

Comments

@vittorius
Copy link

Hello there!

The idea is to either make after_actions be also called when an action early exits via fail_and_return! or to have a special hook for that purpose.
A possible use-case (from a real project I work on) is to e.g. log failed action error messages along with some payload, but in a single place, regardless of whether it was fail or fail_and_return!.

@adomokos
Copy link
Owner

I've just looked up the documentation to remind me how this works.
The after_action hook was created for instrumentation purposes only. We "lifted" this idea from AOP.

You would have to catch the exception in your action, set that exception in the context, and use the after_action hook to "monitor" and "report" the error from the context. The nice thing about this is that you could make the exception handling short in your code, and you could put the logging into the after_action, decoupling how the "reporter" works apart from your business logic.

Hth.

@vittorius
Copy link
Author

vittorius commented Jan 24, 2023

I see. But what if an action didn't fail because of an exception? What if an author has decided to fail early just because some business logic condition hasn't been fulfilled? Looks like I would have to just call next by myself, right?

executed do
  if business_logic_rule_violated?
    ctx[:error] = { message: '...', data: '...' }
    ctx.fail!
    next
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants