class Github::Error::ServiceError

Constants

MIN_BODY_LENGTH

Public Class Methods

error_mapping() click to toggle source

A mapping of status codes and error types

@return [Hash[Integer, Object]]

@api public

# File lib/github_api/error/service_error.rb, line 26
def self.error_mapping
  @error_mapping ||= Hash[
    descendants.map do |klass|
      [klass.new({}).http_status_code, klass]
    end
  ]
end
http_status_code(code) click to toggle source

Add http status code method to error type

@param [Integer] code

the status code

@api public

# File lib/github_api/error/service_error.rb, line 17
def self.http_status_code(code)
  define_method(:http_status_code) { code }
end
new(response) click to toggle source

Create a ServiceError

@param [Hash] response

@api public

Calls superclass method Github::Error::GithubError::new
# File lib/github_api/error/service_error.rb, line 41
def initialize(response)
  @headers = response[:response_headers]
  @body    = response[:body]
  @status  = response[:status]

  @response_headers = @headers
  @response_message = @body

  super(create_message(response))
end

Public Instance Methods

data() click to toggle source

Expose response payload as JSON object if possible

@return [Hash|String]

@api public

# File lib/github_api/error/service_error.rb, line 57
def data
  @data ||= decode_data(@body)
end
error_messages() click to toggle source

Stores error message(s) returned in response body

@return [Array[Hash]]

the array of hash error objects

@api public

# File lib/github_api/error/service_error.rb, line 67
def error_messages
  @error_messages ||= begin
    data[:errors] ? data[:errors] : [data]
  end
end

Private Instance Methods

create_error_summary() click to toggle source

Create error summary from response body

@return [String]

@api private

# File lib/github_api/error/service_error.rb, line 136
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
create_message(response) click to toggle source

Create full error message

@param [Hash] response

the http response

@return [String]

the error message

@api private

# File lib/github_api/error/service_error.rb, line 84
def create_message(response)
  return if response.nil?

  message = "#{response[:method].to_s.upcase} "
  message << "#{response[:url]}: "
  message << "#{@status} - #{format_response}"
  message
end
decode_data(body) click to toggle source

Decode body information if in JSON format

@param [String] body

the response body

@api private

# File lib/github_api/error/service_error.rb, line 99
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
format_response() click to toggle source

Read response body and convert to human friendly format

@return [String]

@api private

# File lib/github_api/error/service_error.rb, line 115
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