class Github::Error::ServiceError

def self.error_mapping

Other tags:
    Api: - public

Returns:
  • (Hash[Integer, Object]) -
def self.error_mapping
  @error_mapping ||= Hash[
    descendants.map do |klass|
      [klass.new({}).http_status_code, klass]
    end
  ]
end

def self.http_status_code(code)

Other tags:
    Api: - public

Parameters:
  • code (Integer) --
def self.http_status_code(code)
  define_method(:http_status_code) { code }
end

def create_error_summary

Other tags:
    Api: - private

Returns:
  • (String) -
def create_error_summary
  if data[:error]
    "\nError: #{data[:error]}"
  elsif data[:errors]
    message = "\nErrors:\n"
    message << data[:errors].map do |error|
      case error
      when Hash
        "Error: #{error.map { |k, v| "#{k}: #{v}" }.join(', ')}"
      else
        "Error: #{error}"
      end
    end.join("\n")
  end
end

def create_message(response)

Other tags:
    Api: - private

Returns:
  • (String) -

Parameters:
  • response (Hash[Symbol]) --
def create_message(response)
  return if response.nil?
  message = "#{response[:method].to_s.upcase} "
  message << "#{response[:url]}: "
  message << "#{@status} - #{format_response}"
  message
end

def data

Other tags:
    Api: - public

Returns:
  • (Hash[Symbol]|String) -
def data
  @data ||= decode_data(@body)
end

def decode_data(body)

Other tags:
    Api: - private

Parameters:
  • body (String) --
def decode_data(body)
  if body.respond_to?(:to_str) &&
     body.length >= MIN_BODY_LENGTH &&
     @headers[:content_type] =~ /json/
    JSON.parse(body, symbolize_names: true)
  else
    body
  end
end

def error_messages

Other tags:
    Api: - public

Returns:
  • (Array[Hash[Symbol]]) -
def error_messages
  @error_messages ||= begin
    data[:errors] ? data[:errors] : [data]
  end
end

def format_response

Other tags:
    Api: - private

Returns:
  • (String) -
def format_response
  return '' if data.nil? || data.empty?
  case data
  when Hash
    message = data[:message] ? data[:message] : ' '
    docs = data[:documentation_url]
    error = create_error_summary
    message << error if error
    message << "\nSee: #{docs}" if docs
    message
  when String
    data
  end
end

def initialize(response)

Other tags:
    Api: - public

Parameters:
  • response (Hash[Symbol]) --
def initialize(response)
  @headers = response[:response_headers]
  @body    = response[:body]
  @status  = response[:status]
  @response_headers = @headers
  @response_message = @body
  super(create_message(response))
end