Dry-transaction invalid result from around step adapter


#1

So I am following http://dry-rb.org/gems/dry-transaction/around-steps/ and i have decided to try out transactions in my app

my transaction looks something like this

class MyTransaction
  include Dry::Transaction(container: MyContainer)

  around :transaction, wtih: 'transaction'
  step :create_one
  step :create_two
end

and my around transaction looks like this from my container

  register('transaction') do |input, &block|
    result = nil

    begin
      ActiveRecord::Base.transaction do
        binding.pry
        result = block.call(Success.new(input))
        binding.pry
        raise ActiveRecord::Rollback if result.failure?
        result
      end
    rescue ActiveRecord::Rollback
      # the code never reached here...
      result
    end
  end

it works fine when there are no failures, but when a failure occurs… I get this error Dry::Transaction::InvalidResultError (step +transaction+ must return a Result object) from the around transaction… i tried debugging to see what seems to be the cause but control doesn’t seem to enter the rescue block

did i do something wrong with my around step?


#2

Turns out it was an error with my usage of ActiveRecord::Rollback rather than an issue with dry-transaction

I would like to request to have this thread closed. thanks!


#3

@SheepD did you use something like that with Rails ?

def db_transaction(input, &block)
        result = nil

        ActiveRecord::Base.transaction do
          result = block.(Success(input))
          raise ActiveRecord::Rollback if result.failure?
        end

        result
      end

#4

thanks @eduardodeoh, that is a better solution than what i came up with.