Skip to content

Commit dceb5e1

Browse files
committed
Change to expose data and error_messages in reference to issue piotrmurach#273
1 parent 91a62f0 commit dceb5e1

2 files changed

Lines changed: 36 additions & 18 deletions

File tree

lib/github_api/error/service_error.rb

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ def self.error_mapping
3131

3232
MIN_BODY_LENGTH = 2
3333

34-
attr_reader :errors
35-
3634
# Crate a ServiceError
3735
#
3836
# @param [Hash[Symbol]] response
@@ -49,6 +47,27 @@ def initialize(response)
4947
super(create_message(response))
5048
end
5149

50+
# Expose response payload as JSON object if possible
51+
#
52+
# @return [Hash[Symbol]|String]
53+
#
54+
# @api public
55+
def data
56+
@data ||= decode_data(@body)
57+
end
58+
59+
# Stores error message(s) returned in response body
60+
#
61+
# @return [Array[Hash[Symbol]]]
62+
# the array of hash error objects
63+
#
64+
# @api public
65+
def error_messages
66+
@error_messages ||= begin
67+
data[:errors] ? data[:errors] : [data]
68+
end
69+
end
70+
5271
private
5372

5473
# Create full error message
@@ -65,7 +84,7 @@ def create_message(response)
6584

6685
message = "#{response[:method].to_s.upcase} "
6786
message << "#{response[:url]}: "
68-
message << "#{@status} - #{parse_body(@body)}"
87+
message << "#{@status} - #{format_response}"
6988
message
7089
end
7190

@@ -86,22 +105,23 @@ def decode_data(body)
86105
end
87106
end
88107

108+
# Read response body and convert to human friendly format
109+
#
110+
# @return [String]
111+
#
89112
# @api private
90-
def parse_body(body)
91-
data = decode_data(body)
92-
113+
def format_response
93114
return '' if data.nil? || data.empty?
94115

95116
case data
96117
when Hash
97118
message = data[:message] ? data[:message] : ' '
98119
docs = data[:documentation_url]
99-
error = create_error_summary(data)
120+
error = create_error_summary
100121
message << error if error
101122
message << "\nSee: #{docs}" if docs
102123
message
103124
when String
104-
@errors = [data]
105125
data
106126
end
107127
end
@@ -111,19 +131,14 @@ def parse_body(body)
111131
# @return [String]
112132
#
113133
# @api private
114-
def create_error_summary(data)
134+
def create_error_summary
115135
if data[:error]
116-
@errors = [data]
117-
return "\nError: #{data[:error]}"
136+
"\nError: #{data[:error]}"
118137
elsif data[:errors]
119-
@errors = data[:errors]
120138
message = "\nErrors:\n"
121139
message << data[:errors].map do |error|
122140
"Error: #{error.map { |k, v| "#{k}: #{v}" }.join(', ')}"
123141
end.join("\n")
124-
else
125-
@errors = [data]
126-
nil
127142
end
128143
end
129144
end # ServiceError

spec/unit/error/service_error/new_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def create_response(body)
2727
expect(error.to_s).to eq("POST #{url}: #{status} - Requires authentication")
2828
expect(error.response_headers).to eql(response_headers)
2929
expect(error.response_message).to eql(body)
30-
expect(error.errors).to eql([{ message: 'Requires authentication' }])
30+
expect(error.error_messages).to eql([{ message: 'Requires authentication' }])
3131
end
3232

3333
it "creates message with single error summary" do
@@ -43,7 +43,7 @@ def create_response(body)
4343
].join("\n"))
4444
expect(error.response_headers).to eql(response_headers)
4545
expect(error.response_message).to eql(body)
46-
expect(error.errors).to eql([{:message=>"Validation Failed\nError: No commits between master and noexist\nSee: https://developer.github.com/enterprise/2.6/v3/pulls/#create-a-pull-request", :error=>"No commits between master and noexist", :documentation_url=>"https://developer.github.com/enterprise/2.6/v3/pulls/#create-a-pull-request"}])
46+
expect(error.error_messages).to eql([{:message=>"Validation Failed\nError: No commits between master and noexist\nSee: https://developer.github.com/enterprise/2.6/v3/pulls/#create-a-pull-request", :error=>"No commits between master and noexist", :documentation_url=>"https://developer.github.com/enterprise/2.6/v3/pulls/#create-a-pull-request"}])
4747
end
4848

4949
it "creates message with multiple errors summary" do
@@ -62,6 +62,9 @@ def create_response(body)
6262
].join("\n"))
6363
expect(error.response_headers).to eql(response_headers)
6464
expect(error.response_message).to eql(body)
65-
expect(error.errors).to eql([{:resource=>"PullRequest", :code=>"missing_field", :field=>"head_sha"}, {:resource=>"PullRequest", :code=>"missing_field", :field=>"base_sha"}, {:resource=>"PullRequest", :code=>"custom", :message=>"No commits between master and noexist"}])
65+
expect(error.error_messages).to eql([
66+
{:resource=>"PullRequest", :code=>"missing_field", :field=>"head_sha"},
67+
{:resource=>"PullRequest", :code=>"missing_field", :field=>"base_sha"},
68+
{:resource=>"PullRequest", :code=>"custom", :message=>"No commits between master and noexist"}])
6669
end
6770
end # Github::Error::ServiceError

0 commit comments

Comments
 (0)