grape icon indicating copy to clipboard operation
grape copied to clipboard

DEPRECATION WARNING: Using `return`, `break` or `throw` to exit a transaction block

Open bobyin opened this issue 4 years ago • 5 comments

Hi

We hit this problem when upgrade rails to 6.1.4, found the error! function throw it, please help. Thanks

DEPRECATION WARNING: Using return, break or throw to exit a transaction block is deprecated without replacement. If the throw came from Timeout.timeout(duration), pass an exception class as a second argument so it doesn't use throw to abort its block. This results in the transaction being committed, but in the next release of Rails it will rollback.

Screen Shot 2021-11-23 at 10 12 57 AM

bobyin avatar Nov 23 '21 02:11 bobyin

i'm not sure this code is part of ruby-grape anymore

goatandsheep avatar Sep 06 '23 18:09 goatandsheep

@goatandsheep It seems like the code is in grape, at https://github.com/ruby-grape/grape/blob/master/lib/grape/dsl/inside_route.rb#L164-L168:

  def error!(message, status = nil, additional_headers = nil)
    status = self.status(status || namespace_inheritable(:default_error_status))
    headers = additional_headers.present? ? header.merge(additional_headers) : header
    throw :error, message: message, status: status, headers: headers
  end

I'm getting this warning in Rails 7.1:

DEPRECATION WARNING: A transaction is being rolled back because the transaction block was
exited using `return`, `break` or `throw`.
In Rails 7.2 this transaction will be committed instead.
To opt-in to the new behavior now and suppress this warning
you can set:

  Rails.application.config.active_record.commit_transaction_on_non_local_return = true

Even if I set commit_transaction_on_non_local_return to false, I'm not seeing where that has an effect in Rails' future, in the main branch. And the commit_transaction_on_non_local_return config option itself states it will be removed in Rails 7.3.

Can this be fixed so that error!() stops a transaction? Should instead the README state that error()! used to roll back a transaction, but it won't do that in Rails 7.2+?

houhoulis avatar Mar 04 '24 21:03 houhoulis

Someone who understands what Rails is trying to accomplish here should pitch in ;)

dblock avatar Mar 05 '24 03:03 dblock