Sha256: fe9d9c34fbf385b9816714ed6ffcf0fa2ba33430cf7c48e2f76d08efda13a985

Contents?: true

Size: 1.86 KB

Versions: 9

Compression:

Stored size: 1.86 KB

Contents

module Deas

  class ErrorHandler

    def self.run(*args)
      self.new(*args).run
    end

    attr_reader :exception, :context, :error_procs

    def initialize(exception, context_hash)
      @exception   = exception
      @context     = Context.new(context_hash)
      @error_procs = context_hash[:server_data].error_procs.reverse
    end

    # The exception that we are generating a response for can change in the case
    # that the configured error proc raises an exception. If this occurs, a
    # response will be generated for that exception, instead of the original
    # one. This is designed to avoid "hidden" errors happening, this way the
    # server will respond and log based on the last exception that occurred.

    def run
      @error_procs.inject(nil) do |response, error_proc|
        result = begin
          error_proc.call(@exception, @context)
        rescue StandardError => proc_exception
          @exception = proc_exception
          response = nil # reset response
        end
        response || result
      end
    end

    class Context

      attr_reader :server_data
      attr_reader :request, :response, :handler_class, :handler, :params

      def initialize(args)
        @server_data   = args[:server_data]
        @request       = args[:request]
        @response      = args[:response]
        @handler_class = args[:handler_class]
        @handler       = args[:handler]
        @params        = args[:params]
      end

      def ==(other)
        if other.kind_of?(self.class)
          self.server_data   == other.server_data &&
          self.handler_class == other.handler_class &&
          self.request       == other.request &&
          self.response      == other.response &&
          self.handler       == other.handler &&
          self.params        == other.params
        else
          super
        end
      end

    end

  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
deas-0.42.0 lib/deas/error_handler.rb
deas-0.41.0 lib/deas/error_handler.rb
deas-0.40.0 lib/deas/error_handler.rb
deas-0.39.2 lib/deas/error_handler.rb
deas-0.39.1 lib/deas/error_handler.rb
deas-0.39.0 lib/deas/error_handler.rb
deas-0.38.0 lib/deas/error_handler.rb
deas-0.37.1 lib/deas/error_handler.rb
deas-0.37.0 lib/deas/error_handler.rb