Exception: Github::Error::ServiceError

Inherits:
GithubError
  • Object
show all
Defined in:
lib/github_api2/error/service_error.rb

Constant Summary collapse

MIN_BODY_LENGTH =
2

Instance Attribute Summary

Attributes inherited from GithubError

#response_headers, #response_message

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GithubError

#backtrace

Constructor Details

#initialize(response) ⇒ ServiceError

Create a ServiceError

Parameters:

  • response (Hash[Symbol])


41
42
43
44
45
46
47
48
49
50
# File 'lib/github_api2/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

Class Method Details

.error_mappingHash[Integer, Object]

A mapping of status codes and error types

Returns:

  • (Hash[Integer, Object])


26
27
28
29
30
31
32
# File 'lib/github_api2/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) ⇒ Object

Add http status code method to error type

Parameters:

  • code (Integer)

    the status code



17
18
19
# File 'lib/github_api2/error/service_error.rb', line 17

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

Instance Method Details

#dataHash[Symbol]|String

Expose response payload as JSON object if possible

Returns:

  • (Hash[Symbol]|String)


57
58
59
# File 'lib/github_api2/error/service_error.rb', line 57

def data
  @data ||= decode_data(@body)
end

#error_messagesArray[Hash[Symbol]]

Stores error message(s) returned in response body

Returns:

  • (Array[Hash[Symbol]])

    the array of hash error objects



67
68
69
70
71
# File 'lib/github_api2/error/service_error.rb', line 67

def error_messages
  @error_messages ||= begin
    data[:errors] ? data[:errors] : [data]
  end
end