Exception: Github::Error::ServiceError

Inherits:
GithubError
  • Object
show all
Defined in:
lib/github_api/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

Crate a ServiceError

Parameters:

  • response (Hash[Symbol])

39
40
41
42
43
44
45
46
47
48
# File 'lib/github_api/error/service_error.rb', line 39

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])

24
25
26
27
28
29
30
# File 'lib/github_api/error/service_error.rb', line 24

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


15
16
17
# File 'lib/github_api/error/service_error.rb', line 15

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)

55
56
57
# File 'lib/github_api/error/service_error.rb', line 55

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


65
66
67
68
69
# File 'lib/github_api/error/service_error.rb', line 65

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