Sha256: e9de074f883e9a12e1339cf7dcaec5bdeb12efe89b7af71c4a133b685dcf76f4

Contents?: true

Size: 746 Bytes

Versions: 17

Compression:

Stored size: 746 Bytes

Contents

class Rack::App::ErrorHandler

  attr_reader :handlers

  def initialize
    @handlers = {}
  end

  def register_handler(exception_classes, handler_block)
    exception_classes.each do |exception_class|
      @handlers[exception_class]= handler_block
    end
    nil
  end

  def execute_with_error_handling_for(instance, &block)
    block.call 
  rescue *[Exception, @handlers.keys].flatten => ex
    instance.instance_exec(ex, &get_handler(ex))
  end

  protected

  def get_handler(ex)
    explicit(ex) || parent(ex) || raise(ex)
  end

  def explicit(ex)
    @handlers[ex.class]
  end

  def parent(ex)
    handler = @handlers.find { |exception_class, _| ex.class <= exception_class }
    return handler.nil? ? nil : handler.last
  end

end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
rack-app-5.10.1 lib/rack/app/error_handler.rb
rack-app-5.10.0 lib/rack/app/error_handler.rb
rack-app-5.9.0 lib/rack/app/error_handler.rb
rack-app-5.8.0 lib/rack/app/error_handler.rb
rack-app-5.7.0 lib/rack/app/error_handler.rb
rack-app-5.7.0.rc lib/rack/app/error_handler.rb
rack-app-5.6.0 lib/rack/app/error_handler.rb
rack-app-5.5.1 lib/rack/app/error_handler.rb
rack-app-5.4.0 lib/rack/app/error_handler.rb
rack-app-5.3.2 lib/rack/app/error_handler.rb
rack-app-5.3.1 lib/rack/app/error_handler.rb
rack-app-5.3.0 lib/rack/app/error_handler.rb
rack-app-5.2.0 lib/rack/app/error_handler.rb
rack-app-5.1.0 lib/rack/app/error_handler.rb
rack-app-5.0.0 lib/rack/app/error_handler.rb
rack-app-5.0.0.rc2 lib/rack/app/error_handler.rb
rack-app-5.0.0.rc1 lib/rack/app/error_handler.rb