This project is archived and is in readonly mode.

#5089 ✓stale
hjdivad

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

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

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

Comments and changes to this ticket

  • hjdivad

    hjdivad July 12th, 2010 @ 03:24 AM

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

    hjdivad July 12th, 2010 @ 03:24 AM

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

    hjdivad July 12th, 2010 @ 03:25 AM

    • 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]”
  • Michael Koziarski

    Michael Koziarski July 12th, 2010 @ 03:53 AM

    • Assigned user set to “Yehuda Katz (wycats)”
    • Importance changed from “” to “Low”
  • hjdivad

    hjdivad July 15th, 2010 @ 09:44 PM

    • 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
    
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:36 PM

    • 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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:36 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages