Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to stop swallowing errors on callbacks. #16537

Merged
merged 1 commit into from
Aug 18, 2014

Conversation

arthurnn
Copy link
Member

Seconds take of #14488 .

After the transaction refactor, we can cleanly apply this patch, as we have a better control of the stack of transactions, and we know that even when raising errors, the transaction wont leaky into the stack.

Description

Add option to stop swallowing standard errors on after_commit and after_rollback.
Currently the option will be +false+ and throw a warning untill set it to +true+.

Example:

# For not swallow errors in after_commit/after_rollback callbacks.
config.active_record.errors_in_transactional_callbacks = true

The option should be removed in future versions of Rails, as the desired
behaviour is not to swallow exceptions.

[fixes #13460]

review @jonleighton @rafaelfranca @tenderlove @chancancode @senny

@@ -375,6 +363,21 @@ def restore_transaction_record_state(force = false) #:nodoc:
end
end

protected
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code didnt change, just had to move it, so I can have access to clear_transaction_record_state force_clear_transaction_record_state restore_transaction_record_state

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

@arthurnn
Copy link
Member Author

@chancancode I guess I addressed all your concerns:

  • Remove unnecessary true
  • Emit warning when the definition of after_commit/after_rollback happens.
  • Add extra argument to rolledback! and committed to not run callbacks. (change code to use them, and dont change visibility of internal methods)
  • Change option to errors_in_transactional_callbacks and a bool.
  • Warning will always show until the option is set to true.
  • Re-wording of the warning message.

@@ -3,11 +3,21 @@ module ActiveRecord
module Transactions
extend ActiveSupport::Concern
ACTIONS = [:create, :destroy, :update]
CALLBACK_WARN_MESSAGE = <<-EOF
Currently `config.active_record.raise_in_transactional_callbacks` is set to false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll start with describing the behavior (i.e. errors are only logged) and the future change. The "option" is really just an opt-in flag and should be mentioned last. (Same for the changelog entry)

But we can also improve this after merging.

Currently, Active Record will rescue any errors raised within
after_rollback/after_create callbacks and print them to the
logs. Next versions of rails will not rescue those errors anymore,
and just bubble them up, as the other callbacks.

This adds a opt-in flag to enable that behaviour, of not rescuing
the errors.
    Example:
      # For not swallow errors in after_commit/after_rollback
      config.active_record.errors_in_transactional_callbacks = true

[fixes rails#13460]
@arthurnn
Copy link
Member Author

@chancancode Warning message, CHANGELOG, and commit message updated.

chancancode added a commit that referenced this pull request Aug 18, 2014
Add option to stop swallowing errors on callbacks.
@chancancode chancancode merged commit 879dde9 into rails:master Aug 18, 2014
@arthurnn arthurnn deleted the stop_swallowing_errors_2 branch August 18, 2014 19:22
@@ -63,13 +67,19 @@ def rollback
end

def rollback_records
records.uniq.each do |record|
ite = records.uniq
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ite an acronym for something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iterator ...

@jonleighton
Copy link
Member

Perhaps new apps should be generated with the config option already set?

@chancancode
Copy link
Member

@jonleighton yep, it's been added in c7c2df2

(Thanks for taking your time to help review this by the way!)

pjambet added a commit to harrystech/rails-pipeline that referenced this pull request Oct 31, 2014
By default Rails swallows exception in after_{commit/rollback} hooks,
therefore prevent specs to fail.
By moving the callbacks inside the transaction in test environment any
exceptions thrown will correctly be bubbled up.

There is an option to fix that in rails 4.2
rails/rails#16537
faun added a commit to zinedistro/zinedistro that referenced this pull request Jan 29, 2015
See [#14488] [1] and [#16537] [2] for more details.

[1]: [rails/rails#14488]
[2]: [rails/rails#16537]
faun added a commit to zinedistro/zinedistro that referenced this pull request Jan 29, 2015
See [#14488] and [#16537] for more details.

[#14488]: [rails/rails#14488]
[#16537]: [rails/rails#16537]
faun added a commit to zinedistro/zinedistro that referenced this pull request Jan 29, 2015
See [#14488] and [#16537] for more details.

[#14488]: [rails/rails#14488]
[#16537]: [rails/rails#16537]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stop swallowing exceptions fromafter_commit callbacks
3 participants