Sha256: 6e0a688cb2b8ccbf26d49e1a8e8cc9ab5025c2045aa6cb59e1920a709f6bf515

Contents?: true

Size: 951 Bytes

Versions: 14

Compression:

Stored size: 951 Bytes

Contents

module Jeanine
  module Rescuable
    def self.included(base)
      base.extend ClassMethods
      base.prepend InstanceMethods
    end

    module InstanceMethods
      def call(env)
        begin
          super
        rescue => error
          handler = self.class.rescue_handlers[error.class.to_s]
          raise error unless handler
          if handler.is_a?(Symbol)
            @response.write(send(handler, error))
          else
            @response.write(instance_exec(error, &handler))
          end
          @response.complete!
        end
      end
    end

    module ClassMethods
      def rescue_from(*exceptions, with: nil, &block)
        exceptions.each do |exception|
          if with
            rescue_handlers[exception.to_s] = with
          else
            rescue_handlers[exception.to_s] = block
          end
        end
      end

      def rescue_handlers
        @rescue_handlers ||= {}
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
jeanine-0.8.1 lib/jeanine/rescuable.rb
jeanine-0.8 lib/jeanine/rescuable.rb
jeanine-0.7.8 lib/jeanine/rescuable.rb
jeanine-0.7.7 lib/jeanine/rescuable.rb
jeanine-0.7.6 lib/jeanine/rescuable.rb
jeanine-0.7.5 lib/jeanine/rescuable.rb
jeanine-0.7.4 lib/jeanine/rescuable.rb
jeanine-0.7.3.2 lib/jeanine/rescuable.rb
jeanine-0.7.3.1 lib/jeanine/rescuable.rb
jeanine-0.7.3 lib/jeanine/rescuable.rb
jeanine-0.7.2 lib/jeanine/rescuable.rb
jeanine-0.7.1 lib/jeanine/rescuable.rb
jeanine-0.7.0 lib/jeanine/rescuable.rb
jeanine-0.6.0 lib/jeanine/rescuable.rb