You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.success? still reporting true when interactor invoked via .call is calling internally another interactor with .call! and latter fails #170
I've noticed in our code base that we had cases where Interactor was encapsulating more interactors and calling them via .call!. One of the interactors was raising Interactor::Failure exception, but because parent interactor was invoked via .call context .success? was still reporting true.
I believe that is wrong behavior and since .call is not raising exception it at least should return false when .success? is invoked.
Steps to reproduce
classParentInteractorincludeInteractordefcallInteractorWithError.call!endendclassInteractorWithErrorincludeInteractordefcallraiseInteractor::Failure.new('test')endendtest'interactor still reports success after it failed'docontext=ParentInteractor.callassertcontext.failure?assert_notcontext.success?end
Reacted by Adam Duffy, Sunny Ripert, Sergiy Kukunin, Nikolay Markov and Lekë Mula
changed the title [-].success? still reporting true when interactor invoked via .call is calling internally another interactor with .call! [/-][+].success? still reporting true when interactor invoked via .call is calling internally another interactor with .call! and latter fails[/+]on Jun 19, 2019
If I understood correctly, this example is trying to fail context in an abnormal way. You're supposed to use the Context API, like context.fail! (which in its turn will raise the Failure error). This example bypasses the Context#fail! method, trying to raise the error by its own. The error class is supposed to be used internally. Notice, though, that you're not even failing the context object of the nested interactor. Try:
class InteractorWithError
include Interactor
def call
raise Interactor::Failure.new('test')
end
end
ctx = InteractorWithError.call
ctx.success?
#=> true
The second issue in this example is that the context object of the parent interactor is not being shared with the nested interactor. Therefore, the context of the parent interactor remains untouched after the call of the nested one (which also doesn't fail context as mentioned in the above example).
A more correct version of what you're trying to do would be:
class ParentInteractor
include Interactor
def call
InteractorWithError.call!(context)
end
end
class InteractorWithError
include Interactor
def call
context.fail!
end
end
ctx = ParentInteractor.call
ctx.success?
#=> false
I'd recommend reading the Context class documentation just to clarify what I tried to explain here.
Hope it helps! =)
I think the current way breaks the idea that call! should raise an exception on failures. Calling InteractorWithError.call!(context) may be more "correct" but it isn't very handy when the children interactors need to accept different arguments or be applied on a collection.
We ended up adding the following to our interactors:
Sadly we still have to think to wrap every call! inside interactors with handle_failures do … end.
I think it would make more sense to either have the Interactor::Failure exception get rethrown or, if it is rescued, at least fail the parent interactor.
I appreciate what @tiagofsilva was getting at about correct usage, but sometimes nesting of Interactors is dynamic, indirect, or generally requires the caller to be aware of the callee's implementation.
In #192 I think we have a fix for this and generally unexpected behavior of nested Interactors.
This behavior is actually dangerous because it's pretty expected that internal Interactor.call! exceptions will be bubbled up to the top, and won't be swallowed. Exceptions might remain unnoticed. +1 for changing the current behavior
Reacted by Sunny Ripert, Adriano Rob and David Mora
Activity
[-].success? still reporting true when interactor invoked via .call is calling internally another interactor with .call! [/-][+].success? still reporting true when interactor invoked via .call is calling internally another interactor with .call! and latter fails[/+]tiagofsilva commentedon Jul 9, 2019
If I understood correctly, this example is trying to fail context in an abnormal way. You're supposed to use the Context API, like
context.fail!(which in its turn will raise the Failure error). This example bypasses theContext#fail!method, trying to raise the error by its own. The error class is supposed to be used internally. Notice, though, that you're not even failing the context object of the nested interactor. Try:The second issue in this example is that the context object of the parent interactor is not being shared with the nested interactor. Therefore, the context of the parent interactor remains untouched after the call of the nested one (which also doesn't fail context as mentioned in the above example).
A more correct version of what you're trying to do would be:
I'd recommend reading the Context class documentation just to clarify what I tried to explain here.
Hope it helps! =)
sunny commentedon Dec 10, 2019
I think the current way breaks the idea that
call!should raise an exception on failures. CallingInteractorWithError.call!(context)may be more "correct" but it isn't very handy when the children interactors need to accept different arguments or be applied on a collection.We ended up adding the following to our interactors:
Sadly we still have to think to wrap every
call!inside interactors withhandle_failures do … end.I think it would make more sense to either have the
Interactor::Failureexception get rethrown or, if it is rescued, at least fail the parent interactor.Don't rescue Failures arising from other contexts.
jqr commentedon Dec 3, 2020
I appreciate what @tiagofsilva was getting at about correct usage, but sometimes nesting of Interactors is dynamic, indirect, or generally requires the caller to be aware of the callee's implementation.
In #192 I think we have a fix for this and generally unexpected behavior of nested Interactors.
Kukunin commentedon Sep 9, 2021
This behavior is actually dangerous because it's pretty expected that internal Interactor.call! exceptions will be bubbled up to the top, and won't be swallowed. Exceptions might remain unnoticed. +1 for changing the current behavior
Add changelog entry for #170 (PR #190)