forked from piotrmurach/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_error.rb
More file actions
65 lines (54 loc) · 1.48 KB
/
Copy pathservice_error.rb
File metadata and controls
65 lines (54 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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