Sha256: 7517e5b1fa2e6f25cce5756eec2ec7390a96bccda96aab70aba3b86b6bbba604

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# Sanford's exception handler class takes an exception and builds a valid
# response. For certain exceptions, Sanford will use special response codes and
# for all others it will classify them as generic error requests.
#
require 'sanford-protocol'

require 'sanford/exceptions'

module Sanford

  class ExceptionHandler
    attr_reader :exception, :logger

    def initialize(exception, logger)
      @exception = exception
      @logger = logger
    end

    def response
      self.logger.error("#{exception.class}: #{exception.message}")
      self.logger.error(exception.backtrace.join("\n"))
      status = Sanford::Protocol::ResponseStatus.new(*self.determine_code_and_message)
      Sanford::Protocol::Response.new(status)
    end

    protected

    def determine_code_and_message
      case(self.exception)
      when Sanford::Protocol::BadMessageError, Sanford::Protocol::BadRequestError
        [ :bad_request, self.exception.message ]
      when Sanford::NotFoundError
        [ :not_found ]
      when Sanford::Protocol::TimeoutError
        [ :timeout ]
      when Exception
        [ :error, "An unexpected error occurred." ]
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sanford-0.1.0 lib/sanford/exception_handler.rb