Skip to content

Commit 42ecefc

Browse files
committed
Change service error to simplify parsing.
1 parent a1c33ac commit 42ecefc

2 files changed

Lines changed: 21 additions & 23 deletions

File tree

lib/github_api/error/service_error.rb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,25 @@ module Github
66
# Raised when GitHub returns any of the HTTP status codes
77
module Error
88
class ServiceError < GithubError
9-
109
attr_reader :http_headers, :body, :status
1110

1211
MIN_BODY_LENGTH = 2
1312

1413
def initialize(response)
1514
@http_headers = response[:response_headers]
16-
message = parse_response(response)
17-
super(message)
15+
@body = parse_body(response[:body])
16+
@status = response[:status]
17+
18+
super(create_message(response))
1819
end
1920

20-
def parse_response(response)
21-
@body = parse_body(response[:body])
22-
@status = response[:status]
23-
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{@status} #{@body}"
21+
def create_message(response)
22+
"#{response[:method].to_s.upcase} #{response[:url]}: #{@status} #{@body}"
2423
end
2524

2625
def decode_body(body)
2726
if body.respond_to?(:to_str) && body.length >= MIN_BODY_LENGTH
28-
JSON.parse(body, symbolize_names: true)
27+
JSON.parse(body, symbolize_names: true)
2928
else
3029
body
3130
end
@@ -40,11 +39,7 @@ def parse_body(body)
4039
body[:error]
4140
elsif body[:errors]
4241
error = Array(body[:errors]).first
43-
if error.kind_of?(Hash)
44-
error[:message]
45-
else
46-
error
47-
end
42+
error.is_a?(Hash) ? error[:message] : error
4843
elsif body[:message]
4944
body[:message]
5045
else

spec/github/error/service_error/parse_response_spec.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'spec_helper'
44

5-
describe Github::Error::ServiceError, 'parse_response' do
5+
RSpec.describe Github::Error::ServiceError, '#new' do
66
let(:message) { 'Requires authentication' }
77
let(:url) { 'https://api.github.com/user/repos' }
88
let(:body) { "{\"message\":\"#{message}\"}" }
@@ -15,24 +15,27 @@
1515
url: url
1616
}}
1717

18-
let(:object) { described_class.new(response) }
19-
20-
subject { object.parse_response(response) }
21-
2218
it "parses body" do
23-
expect(object).to receive(:parse_body).with(body)
24-
subject
19+
error = described_class.new(response)
20+
21+
expect(error.body).to eq(message)
2522
end
2623

2724
it "parses http headers" do
28-
expect(object.http_headers).to eql(response_headers)
25+
error = described_class.new(response)
26+
27+
expect(error.http_headers).to eq(response_headers)
2928
end
3029

3130
it "parses status" do
32-
expect(object.status).to eql(status)
31+
error = described_class.new(response)
32+
33+
expect(error.status).to eq(status)
3334
end
3435

3536
it "assembles error message" do
36-
expect(subject).to eql(" #{url}: #{status} #{message}")
37+
error = described_class.new(response)
38+
39+
expect(error.message).to eql(" #{url}: #{status} #{message}")
3740
end
3841
end # Github::Error::ServiceError

0 commit comments

Comments
 (0)