Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveSupport::Callbacks :rescuable => true does not work for errors in :before callbacks [Patch]

#5089

Steps To Reproduce

require 'active_support'
require 'active_support/callbacks'

class Foo
  include ActiveSupport::Callbacks
  define_callbacks :event, :rescuable => true

  set_callback :event, :before do
    raise "Error"
  end
  set_callback :event, :after do
    puts "After callback still run."
  end

  def run
    run_callbacks :event
  end
end

Foo.new.run

Expected Behaviour

"After callback still run." should be printed, and then the exception raised. i.e. the behaviour (relative the :after callback) should be the same as for the
following:

require 'active_support'
require 'active_support/callbacks'

class Foo
  include ActiveSupport::Callbacks
  define_callbacks :event, :rescuable => true

  set_callback :event, :after do
    puts "After callback still run."
  end

  def run
    run_callbacks :event do
      raise "Error"
    end
  end
end

Foo.new.run

Actual Behaviour

The string is not printed (i.e. the :after callback is not invoked), but the
exception is raised.

Proposed Patch

See [1] for a github branch with a test case and patch. Alternatively, see
attached patch.

References

  1. http://github.com/hjdivad/rails/tree/issue-5089

Reported by hjdivad · July 12th, 2010 @ 03:21 AM

State: stale
Milestone: none
Assigned to: Yehuda Katz (wycats) Yehuda Katz (wycats)
Importance: Low

Activity

  1. hjdivad
    hjdivad
    • Tag changed from callbacks active_support to active_support, callbacks

    July 12th, 2010 @ 03:24 AM

  2. hjdivad
    hjdivad
    • Tag changed from active_support, callbacks to activesupport, callbacks

    July 12th, 2010 @ 03:24 AM

  3. hjdivad
    hjdivad
    • Title changed from ActiveSupport::Callbacks :rescuable => true does not work for errs in :before callbacks [Patch] to ActiveSupport::Callbacks :rescuable => true does not work for errors in :before callbacks [Patch]

    July 12th, 2010 @ 03:25 AM

  4. Michael Koziarski
    Michael Koziarski
    • Assigned user set to Yehuda Katz (wycats)
    • Importance changed from to Low

    July 12th, 2010 @ 03:53 AM

  5. hjdivad
    hjdivad
    • Tag changed from activesupport, callbacks to activesupport, callbacks, patch

    I have uploaded a better patch than the original, and also updated the git
    branch. My original patch broke callbacks with :before and :around with
    :rescuable => true.

    Also, quite apart from running :after callbacks when :before callbacks raised
    exceptions, it seems there was a general problem with the :rescuable feature
    with :around callbacks that resulted from rescued_error being first declared
    in a block, and thus undefined at the conditional raise.

    def compile(key=nil, object=nil)
      method = []
      method << "value = nil"
      method << "halted = false"
    
    # The :around callback start will result in a method invocation and the
    # beginning of a block...
      each do |callback|
        method << callback.start(key, object)
      end
    
    # This causes rescued_error to be local to that block...
      if config[:rescuable]
        method << "rescued_error = nil"
        method << "begin"
      end
    
      method << "value = yield if block_given? && !halted"
    
      if config[:rescuable]
        method << "rescue Exception => e"
        method << "rescued_error = e"
        method << "end"
      end
    
      reverse_each do |callback|
        method << callback.end(key, object)
      end
    
    # and thus undefined at this line
      method << "raise rescued_error if rescued_error" if config[:rescuable]
      method << "halted ? false : (block_given? ? value : true)"
      method.compact.join("\n")
    end
    

    July 15th, 2010 @ 09:44 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:36 PM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:36 PM