adomokos / light-service

Series of Actions with an emphasis on simplicity.
MIT License
837 stars 67 forks source link

after_fail callback #127

Closed rudyrigot closed 6 years ago

rudyrigot commented 6 years ago

To make my test failures more meaningful when asserting result.success?, I'd like to throw an error when ctx.fail! is called in my test environment. I believe this is best done by adding a callback/hook to an orchestrator that would run when the orchestrator fails. I'm opening this issue to present an idea of how that could work, and welcome people to discuss it (and then I'd be glad to implement it).

Would we rather have something naive like this:

class CalculatesTax
  extend LightService::Organizer

  def self.call(order)
    with(:order => order).reduce(
        LooksUpTaxPercentageAction,
        CalculatesOrderTaxAction,
        ProvidesFreeShippingAction
      )
  end

  def self.after_fail
    # Do something
  end
end

or something a bit more like Active Record does callbacks, like this:

class CalculatesTax
  extend LightService::Organizer

  after_fail :do_something

  def self.call(order)
    with(:order => order).reduce(
        LooksUpTaxPercentageAction,
        CalculatesOrderTaxAction,
        ProvidesFreeShippingAction
      )
  end

  def self.do_something
    # Do something
  end
end

I'm not sure a given orchestrator is likely to have as many reasons to stack several same-type callbacks on top of each other, so I'm not sure anything beyond the naive solution is needed; but I'd welcome people's take about it.

rudyrigot commented 6 years ago

Closing this because #133 attempts to solve it, conversation can live there.