Skip to content

Commit 730e30a

Browse files
committed
Merge pull request #4 from Cielo24/feature/CLC-1068
CLC-1068: Fix error handling issues
2 parents ebdda1b + e25ae7d commit 730e30a

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

cielo24_gem/cielo24-0.0.17.gem

1 KB
Binary file not shown.

cielo24_gem/cielo24.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
2323
spec.add_dependency 'httpclient', '~> 2.6.0.1'
2424
spec.add_dependency 'hashie', '~> 3.4.2'
2525
spec.add_dependency 'tzinfo', '~> 1.2.2'
26+
spec.add_dependency 'tzinfo-data', '~> 1.2015.7'
2627
end

cielo24_gem/lib/cielo24/actions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def aggregate_statistics(api_token, metrics=nil, group_by=nil, start_date=nil, e
223223
# account_id parameter named sub_account for clarity
224224
query_hash[:account_id] = sub_account unless sub_account.nil?
225225
response = WebUtils.get_json(@base_url + AGGREGATE_STATISTICS_PATH, 'GET', WebUtils::BASIC_TIMEOUT, query_hash)
226-
return Mash.new(response)
226+
Mash.new(response)
227227
end
228228

229229
##############################

cielo24_gem/lib/cielo24/web_utils.rb

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class WebUtils
77
require 'logger'
88
require 'tzinfo'
99
include JSON
10+
include TZInfo
1011

1112
BASIC_TIMEOUT = 60 # seconds (1 minute)
1213
DOWNLOAD_TIMEOUT = 300 # seconds (5 minutes)
@@ -16,7 +17,7 @@ class WebUtils
1617

1718
def self.get_json(uri, method, timeout, query=nil, headers=nil, body=nil)
1819
response = http_request(uri, method, timeout, query, headers, body)
19-
return JSON.parse(response)
20+
return json_parse(response)
2021
end
2122

2223
def self.http_request(uri, method, timeout, query=nil, headers=nil, body=nil)
@@ -35,8 +36,8 @@ def self.http_request(uri, method, timeout, query=nil, headers=nil, body=nil)
3536
if response.status_code == 200 or response.status_code == 204
3637
return response.body
3738
else
38-
json = JSON.parse(response.body)
39-
raise WebError.new(json['ErrorType'], json['ErrorComment'])
39+
json = json_parse(response.body)
40+
raise WebError.new(response.status_code, json['ErrorType'], json['ErrorComment'])
4041
end
4142

4243
}
@@ -47,29 +48,55 @@ def self.http_request(uri, method, timeout, query=nil, headers=nil, body=nil)
4748

4849
def self.to_utc(s)
4950
return s if s.empty?
50-
tz = TZInfo::Timezone.get(SERVER_TZ)
51+
tz = Timezone.get(SERVER_TZ)
5152
local = DateTime.iso8601(s)
5253
utc = tz.local_to_utc local
5354
format = '%Y-%m-%dT%H:%M:%S.%L%z' # iso8601 with milliseconds
5455
utc.strftime(format)
5556
end
57+
58+
def self.json_parse(s)
59+
begin
60+
return JSON.parse(s)
61+
rescue JSON::ParserError
62+
raise ParsingError.new("Bad JSON String: \"#{s}\"")
63+
end
64+
end
5665
end
5766

58-
class WebError < StandardError
67+
class CieloError < StandardError
5968
attr_reader :type
6069
def initialize(type, comment)
6170
super(comment)
6271
@type = type
6372
end
6473

6574
def to_s
66-
return @type + ' - ' + super.to_s
75+
"#{@type} - #{super.to_s}"
76+
end
77+
end
78+
79+
class WebError < CieloError
80+
attr_reader :status_code
81+
def initialize(status_code, type, comment)
82+
super(type, comment)
83+
@status_code = status_code
84+
end
85+
86+
def to_s
87+
"#{@status_code}:#{super.to_s}"
88+
end
89+
end
90+
91+
class TimeoutError < CieloError
92+
def initialize(message)
93+
super('TIMEOUT_ERROR', message)
6794
end
6895
end
6996

70-
class TimeoutError < StandardError
97+
class ParsingError < CieloError
7198
def initialize(message)
72-
super(message)
99+
super('PARSING_ERROR', message)
73100
end
74101
end
75102
end

cielo24_gem/test/access_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_logout
3939
fail('Should not be able to use the API with invalid API token')
4040
rescue WebError => e
4141
assert_equal(ErrorType::BAD_API_TOKEN, e.type, 'Unexpected error type')
42+
assert_equal(400, e.status_code, 'Unexpected http status code')
4243
end
4344
end
4445

@@ -62,6 +63,7 @@ def test_remove_api_key
6263
fail('Should not be able to login using invalid API key')
6364
rescue WebError => e
6465
assert_equal(ErrorType::ACCOUNT_UNPRIVILEGED, e.type, 'Unexpected error type')
66+
assert_equal(400, e.status_code, 'Unexpected http status code')
6567
end
6668
end
6769

0 commit comments

Comments
 (0)