Sha256: 288d55a2a278b60478e6550c1cbc29673f89f6e7f68075643fa7f127bc2b8215

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

require 'ostruct'
require 'sanford-protocol'

module Sanford

  class ErrorHandler

    attr_reader :exception, :host_data, :request

    def initialize(exception, host_data, request = nil)
      @exception, @host_data, @request = exception, host_data, request
    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
      begin
        result = @host_data.error_proc.call(@exception, @host_data, @request)
      rescue Exception => proc_exception
        @exception = proc_exception
      end
      self.response_from_proc(result) || self.response_from_exception(@exception)
    end

    protected

    def response_from_proc(result)
      case result
      when Sanford::Protocol::Response
        result
      when Integer, Symbol
        build_response result
      end
    end

    def response_from_exception(exception)
      case(exception)
      when Sanford::Protocol::BadMessageError, Sanford::Protocol::BadRequestError
        build_response :bad_request, :message => exception.message
      when Sanford::NotFoundError
        build_response :not_found
      when Sanford::Protocol::TimeoutError
        build_response :timeout
      when Exception
        build_response :error, :message => "An unexpected error occurred."
      end
    end

    def build_response(status, options = nil)
      options = OpenStruct.new(options || {})
      Sanford::Protocol::Response.new([ status, options.message ], options.data)
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sanford-0.3.0 lib/sanford/error_handler.rb
sanford-0.2.0 lib/sanford/error_handler.rb