Sha256: b1673aee1c224b035e414e9dba40d179d655f155ea168f2f66b55c3f218c9d9d

Contents?: true

Size: 1.11 KB

Versions: 18

Compression:

Stored size: 1.11 KB

Contents

class CC::Service
  class HTTPError < StandardError
    attr_reader :response_body, :status, :params, :endpoint_url
    attr_accessor :user_message

    def initialize(message, env)
      @response_body = env[:body]
      @status = env[:status]
      @params = env[:params]
      @endpoint_url = env[:url].to_s

      super(message)
    end
  end

  class ResponseCheck < Faraday::Response::Middleware
    ErrorStatuses = 400...600

    def on_complete(env)
      if ErrorStatuses === env[:status]
        message = error_message(env) ||
          "API request unsuccessful (#{env[:status]})"

        raise HTTPError.new(message, env)
      end
    end

    private

    def error_message(env)
      # We only handle Jira (or responses which look like Jira's). We will add
      # more logic here over time to account for other service's typical error
      # responses as we see them.
      if env[:response_headers]["content-type"] =~ /application\/json/
        errors = JSON.parse(env[:body])["errors"]
        errors.is_a?(Hash) && errors.values.map(&:capitalize).join(", ")
      end
    rescue JSON::ParserError
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
codeclimate-services-1.11.4 lib/cc/service/response_check.rb
codeclimate-services-1.11.3 lib/cc/service/response_check.rb
codeclimate-services-1.11.2 lib/cc/service/response_check.rb
codeclimate-services-1.11.1 lib/cc/service/response_check.rb
codeclimate-services-1.11.0 lib/cc/service/response_check.rb
codeclimate-services-1.10.1 lib/cc/service/response_check.rb
codeclimate-services-1.10.0 lib/cc/service/response_check.rb
codeclimate-services-1.9.8 lib/cc/service/response_check.rb
codeclimate-services-1.9.7 lib/cc/service/response_check.rb
codeclimate-services-1.9.6 lib/cc/service/response_check.rb
codeclimate-services-1.9.5 lib/cc/service/response_check.rb
codeclimate-services-1.9.4 lib/cc/service/response_check.rb
codeclimate-services-1.9.3 lib/cc/service/response_check.rb
codeclimate-services-1.9.2 lib/cc/service/response_check.rb
codeclimate-services-1.9.1 lib/cc/service/response_check.rb
codeclimate-services-1.9.0 lib/cc/service/response_check.rb
codeclimate-services-1.8.0 lib/cc/service/response_check.rb
codeclimate-services-1.7.0 lib/cc/service/response_check.rb