Sha256: 1b18c9d2b337e4a524b5215016a7c36e490a7a8bfff4703459c7973a4423ce8d

Contents?: true

Size: 1.48 KB

Versions: 27

Compression:

Stored size: 1.48 KB

Contents

# encoding: utf-8
# require 'multi_json'
require 'github_api/jsonable'

module Github
  # Raised when GitHub returns any of the HTTP status codes
  module Error
    class ServiceError < GithubError
      include ::Github::Jsonable

      attr_reader :http_headers

      def initialize(response)
        @http_headers = response[:response_headers]
        message = parse_response(response)
        super(message)
      end

      def parse_response(response)
        body = parse_body(response[:body])
        "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]} #{body}"
      end

      def decode_body(body)
        if body.respond_to?(:to_str) && body.length >= 2
           decode body, :symbolize_keys => true
        else
          body
        end
      end

      def parse_body(body)
        body = decode_body(body)

        return '' if body.nil? || body.empty?

        if body[:error]
          body[:error]
        elsif body[:errors]
          error = Array(body[:errors]).first
          if error.kind_of?(Hash)
            error[:message]
          else
            error
          end
        elsif body[:message]
          body[:message]
        else
          ''
        end
      end

      def self.http_status_code(code)
        define_method(:http_status_code) { code }
      end

      def self.errors
        @errors ||= Hash[
          descendants.map { |klass| [klass.new({}).http_status_code, klass] }
        ]
      end

    end
  end # Error
end # Github

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
github_api-0.11.0 lib/github_api/error/service_error.rb
github_api-0.10.2 lib/github_api/error/service_error.rb
github_api-0.10.1 lib/github_api/error/service_error.rb
github_api-0.10.0 lib/github_api/error/service_error.rb
github_api-0.9.7 lib/github_api/error/service_error.rb
github_api-0.9.6 lib/github_api/error/service_error.rb
github_api-0.9.5 lib/github_api/error/service_error.rb
github_api-0.9.4 lib/github_api/error/service_error.rb
github_api-0.9.3 lib/github_api/error/service_error.rb
github_api-0.9.2 lib/github_api/error/service_error.rb
github_api-0.9.1 lib/github_api/error/service_error.rb
github_api-0.9.0 lib/github_api/error/service_error.rb
github_api-0.8.11 lib/github_api/error/service_error.rb
github_api-0.8.10 lib/github_api/error/service_error.rb
github_api-0.8.9 lib/github_api/error/service_error.rb
github_api-0.8.8 lib/github_api/error/service_error.rb
github_api-0.8.7 lib/github_api/error/service_error.rb
github_api-0.8.6 lib/github_api/error/service_error.rb
github_api-0.8.5 lib/github_api/error/service_error.rb
github_api-0.8.4 lib/github_api/error/service_error.rb