Sha256: 3e25dadecb4f773cf54f9bc43b3a558306cde250c138b8eb3af23622692ab791

Contents?: true

Size: 732 Bytes

Versions: 46

Compression:

Stored size: 732 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)
    yield
  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

46 entries across 46 versions & 2 rubygems

Version Path
rack-app-11.0.2 lib/rack/app/error_handler.rb
rack-app-11.0.1 lib/rack/app/error_handler.rb
rack-app-11.0.0 lib/rack/app/error_handler.rb
rack-app-10.0.0 lib/rack/app/error_handler.rb
rack-app-9.0.0 lib/rack/app/error_handler.rb
rack-app-8.1.0 lib/rack/app/error_handler.rb
rack-app-8.0.1 lib/rack/app/error_handler.rb
rack-app-8.0.0 lib/rack/app/error_handler.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-app-7.7.0/lib/rack/app/error_handler.rb
rack-app-7.7.0 lib/rack/app/error_handler.rb
rack-app-7.6.5 lib/rack/app/error_handler.rb
rack-app-7.6.4 lib/rack/app/error_handler.rb
rack-app-7.6.3 lib/rack/app/error_handler.rb
rack-app-7.6.2 lib/rack/app/error_handler.rb
rack-app-7.6.1 lib/rack/app/error_handler.rb
rack-app-7.6.0 lib/rack/app/error_handler.rb
rack-app-7.5.2 lib/rack/app/error_handler.rb
rack-app-7.5.1 lib/rack/app/error_handler.rb
rack-app-7.5.0 lib/rack/app/error_handler.rb
rack-app-7.4.0 lib/rack/app/error_handler.rb