Skip to content

Commit 7a8b16a

Browse files
committed
add errors
1 parent 4a058d9 commit 7a8b16a

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

lib/shipengine/client/platform.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ def initialize(api_key:, base_url: 'https://simengine.herokuapp.com/jsonrpc', ad
1616
end
1717

1818
def assert_no_platform_errors(response)
19-
puts response.inspect
20-
status = response.status
21-
error = response.body['error'] || {}
22-
status == 400 and
23-
raise ShipEngine::Exceptions::ValidationError, error['message'] || status.to_s
24-
status >= 401 and
25-
raise ShipEngine::Exceptions::ShipEngineError, error['message'] || status.to_s
19+
# puts response.inspect
20+
body = response.body
21+
error = body['error'] || {}
22+
data = error['data']
23+
return unless data
24+
raise ShipEngine::Exceptions::ShipEngineErrorDetailed.new(body['id'], error['message'], data) unless data.nil?
25+
end
2626
end
2727

2828
# create jsonrpc request has

lib/shipengine/exceptions.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module ShipEngine
22
module Exceptions
33
# standard class - network connection error / internal server error /etc
4+
45
class ShipEngineError < StandardError
56
def initialize(message_or_messages)
67
message = message_or_messages.is_a?(Array) ? message_or_messages.join('\n') : message_or_messages
@@ -9,7 +10,14 @@ def initialize(message_or_messages)
910
end
1011

1112
# 400 error, or other "user exceptions"
12-
class ValidationError < ShipEngineError
13+
class ShipEngineErrorDetailed < ShipEngineError
14+
def initialize(request_id, message, data)
15+
super(message)
16+
@request_id = request_id
17+
@source = data['source']
18+
@type = data['type']
19+
@code = data['code']
20+
end
1321
end
1422
end
1523
end

test/client_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require 'test_helper'
2-
2+
require 'pry'
33
describe 'Client test' do
44
it 'Should make a request' do
55
client = ::ShipEngine::PlatformClient.new(api_key: 'abc123')
@@ -13,4 +13,14 @@
1313
success_request = client.make_request('address/validate', params)
1414
assert success_request
1515
end
16+
17+
it 'Should throw Errors' do
18+
client = ::ShipEngine::PlatformClient.new(api_key: 'abc123')
19+
client.make_request('address/validate', { foo: 'invalid request' })
20+
raise 'should not happen'
21+
rescue ShipEngine::Exceptions::ShipEngineError => e
22+
assert e.message.is_a?(String)
23+
else
24+
raise 'should not happen'
25+
end
1626
end

0 commit comments

Comments
 (0)