adomokos / light-service

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

Weird FailWithRollbackError #167

Closed jcchikikomori closed 5 years ago

jcchikikomori commented 5 years ago

context.fail_with_rollback! was just fine even i don't even making records/transactions to database until now.

I don't understand how is this happen. When i used pry, i manually execute that syntax and it just gives me this

LightService::FailWithRollbackError LightService::FailWithRollbackError

Here's my sample code. I was creating a category with series (array) of fields

module Actions
  module Category
    module CustomerService
      class CreateFields
        extend LightService::Action

        expects :category, :fields

        executed do |ctx|
          failed(error: :invalid_field, ctx: ctx) unless ctx.fields.is_a?(Array)

          ctx.fields.each do |field|
            new_field = ctx.category.fields.new(
              name: field[:name],
              data_type: field[:data_type],
              category: "default"
            )
            unless new_field.save
              error = new_field.errors.full_messages.to_sentence
              failed(error: error, ctx: ctx)
            end
          end
        end

        # fail_with_rollback! got errors from Light Service library itself
        # https://github.com/adomokos/light-service/blob/master/spec/acceptance/rollback_spec.rb
        def self.failed(error:, ctx:)
          # Revert changes
          ctx.category&.destroy
          ctx.fail_and_return!(error)
        end

        private_instance_methods :failed
      end
    end
  end
end
ace-subido commented 5 years ago

Can you post the stacktrace?

jcchikikomori commented 5 years ago

I'll close this one, because i used the wrong approach of what scope inside action class should be responsible if that fails

Reference: https://github.com/adomokos/light-service/blob/master/lib/light-service/organizer/with_reducer.rb#L41

jcchikikomori commented 5 years ago

Can you post the stacktrace?

Sorry i deleted the stacktrace file because this issue was already vague for me to explain this as i already fixed the problem ;)

Edit: I use try catch / exception approach on my spec