Sha256: 4dccf548819efbf2a418ca47a89da501ff41b3d2e4e464c2d3a6edf7c31a8c24

Contents?: true

Size: 1.13 KB

Versions: 20

Compression:

Stored size: 1.13 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

20 entries across 20 versions & 1 rubygems

Version Path
codeclimate-services-1.6.1 lib/cc/service/response_check.rb
codeclimate-services-1.6.0 lib/cc/service/response_check.rb
codeclimate-services-1.5.1 lib/cc/service/response_check.rb
codeclimate-services-1.5.0 lib/cc/service/response_check.rb
codeclimate-services-1.4.0 lib/cc/service/response_check.rb
codeclimate-services-1.3.0 lib/cc/service/response_check.rb
codeclimate-services-1.2.0 lib/cc/service/response_check.rb
codeclimate-services-1.1.0 lib/cc/service/response_check.rb
codeclimate-services-1.0.1 lib/cc/service/response_check.rb
codeclimate-services-1.0.0 lib/cc/service/response_check.rb
codeclimate-services-0.6.2 lib/cc/service/response_check.rb
codeclimate-services-0.6.1 lib/cc/service/response_check.rb
codeclimate-services-0.6.0 lib/cc/service/response_check.rb
codeclimate-services-0.5.3 lib/cc/service/response_check.rb
codeclimate-services-0.5.2 lib/cc/service/response_check.rb
codeclimate-services-0.5.1 lib/cc/service/response_check.rb
codeclimate-services-0.5.0 lib/cc/service/response_check.rb
codeclimate-services-0.4.1 lib/cc/service/response_check.rb
codeclimate-services-0.4.0 lib/cc/service/response_check.rb
codeclimate-services-0.3.0 lib/cc/service/response_check.rb