Sha256: 9d0a59ee34faf69ab72c2bf84cacb5df239c20977a2df1c9c0ab96049df8da59

Contents?: true

Size: 1.76 KB

Versions: 12

Compression:

Stored size: 1.76 KB

Contents

require 'gh'

module GH
  class Error < StandardError
    attr_reader :info

    def initialize(error = nil, payload = nil, info = {})
      info   = info.merge(error.info) if error.respond_to? :info and Hash === error.info
      error  = error.error while error.respond_to? :error
      @info  = info.merge(:error => error, :payload => payload)

      if error
        set_backtrace error.backtrace if error.respond_to? :backtrace
        if error.respond_to? :response and error.response
          case response = error.response
          when Hash
            @info[:response_status]  = response[:status]
            @info[:response_headers] = response[:headers]
            @info[:response_body]    = response[:body]
          when Faraday::Response
            @info[:response_status]  = response.status
            @info[:response_headers] = response.headers
            @info[:response_body]    = response.body
          else
            @info[:response]         = response
          end
        end
      end
    end

    def payload
      info[:payload]
    end

    def error
      info[:error]
    end

    def message
      "GH request failed\n" + info.map { |k,v| entry(k,v) }.join("\n")
    end

    private

    def entry(key, value)
      value = "#{value.class}: #{value.message}" if Exception === value
      value = value.inspect unless String === value
      value.gsub!(/"Basic .+"|(client_(?:id|secret)=)[^&\s]+/, '\1[removed]')
      (key.to_s + ": ").ljust(20) + value
    end
  end

  class TokenInvalid < Error
  end

  def self.Error(conditions)
    Module.new do
      define_singleton_method(:===) do |exception|
        return false unless Error === exception and not exception.info.nil?
        conditions.all? { |k,v| v === exception.info[k]}
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
gh-0.18.0 lib/gh/error.rb
gh-0.17.0 lib/gh/error.rb
gh-0.16.0 lib/gh/error.rb
gh-0.16.0.beta1 lib/gh/error.rb
gh-akerl-0.15.1.1 lib/gh/error.rb
gh-akerl-0.15.1 lib/gh/error.rb
gh-0.15.1 lib/gh/error.rb
gh-0.15.0 lib/gh/error.rb
gh-0.14.0 lib/gh/error.rb
gh-0.13.3 lib/gh/error.rb
gh-0.13.2 lib/gh/error.rb
gh-0.13.1 lib/gh/error.rb