Sha256: f9ab420ed1e265417e9a8ff1bc14b3ee07e9d01a02981eda60137aed6163c0c1

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require "delegate"

module Jess
  class HttpClient
    # Wraps a Net::HTTP object to provide exception handling around the
    # `request` method, such that 400 and 500 error codes are translated into
    # appropriate Jess::HttpClient::Error exceptions.
    #
    class ErrorDecorator < SimpleDelegator
      def request(req)
        super.tap do |response|
          raise_if_error_code(response)
        end
      rescue StandardError => e
        handle_exception(e, req)
      end

      private

      def raise_if_error_code(res)
        return if res.is_a?(Net::HTTPSuccess)
        raise BadCredentials, res.message if res.code == "401"
        raise NotFound, res.message if res.code == "404"
        raise ServerError, res.message if res.is_a?(Net::HTTPServerError)
        raise Error, res.message
      end

      def handle_exception(e, req)
        case e
        when IOError, Timeout::Error
          e = ConnectionError.new(e.message)
        when Error
          # pass
        else
          e = Error.new(e.inspect)
        end

        e.uri = req.uri
        e.http_method = req.method
        raise e
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jess-0.1.0 lib/jess/http_client/error_decorator.rb