Design updates with error handling

This commit is contained in:
bterkuile committed 2025-10-18 09:13:12 -05:00
1 parent 4c73608549
commit 4b84df0af9
9 files changed
+124 -38

No files matched your search

+13 -11
View File
@@ -2,6 +2,8 @@
module StrikeMe
module API
# Factory helper object connecting the client to the models
class Account
include BaseClient
@@ -12,14 +14,15 @@ module StrikeMe
def profile
return @profile if @profile.present?
response = client.get profile_url
url = profile_url
response = client.get url
#body = response.body.presence
#body = JSON.parse(body) if body.is_a? String
if response.success?
body = response.body
body = JSON.parse(body) if body.is_a? String
@profile = Profile.new body.merge(account: self)
@profile = Profile.new response.body.merge(account: self)
else
# if you see this, something went wrong, this is test code
@profile = ResponseError.new(profile_url, response)
raise ResponseError.new(url, response.body)
end
end
@@ -36,12 +39,11 @@ module StrikeMe
return @balances if @balances.present?
response = client.get 'balances'
@balances = if response.success?
Balances.new self, response.body
else
# if you see this, something went wrong, this is test code
ResponseError.new('balances', response)
end
if response.success?
@balances = Balances.new self, response.body
else
raise ResponseError.new('balances', response.body)
end
end
def inspect
+4
View File
@@ -21,6 +21,10 @@ module StrikeMe
def to_a
@items.map(&:attributes)
end
def btc
@items.find { |item| item.currency == 'BTC' }
end
end
end
end
+12 -4
View File
@@ -19,13 +19,21 @@ module StrikeMe
end
# cumbersome, but better for later debugging and handling
class ResponseError
attr_reader :url, :faraday_response
class ResponseError < StandardError
attr_reader :url, :response_body, :status, :reason
def initialize(url, faraday_response)
def initialize(url, response_body)
@reason = response_body.try(:[], 'data').try(:[], 'message')
@status = response_body.try(:[], 'data').try(:[], 'status')
super "[#{status}, {}, [\"#{reason}\"]]" if reason
@url = url
@faraday_response = faraday_response
@response_body = response_body
end
# def inspect
# "StrikeMe::API::BaseClient::ResponseError: #{url} status: #{faraday_response.status}"
# end
end
end
end
+6 -3
View File
@@ -3,9 +3,7 @@
module StrikeMe
module API
class Client
attr_reader :version, :connection
delegate :get, to: :connection
attr_reader :version, :connection, :last_response
def initialize(**args)
@version = args[:version] || 'v1'
@@ -23,6 +21,11 @@ module StrikeMe
builder.response :json
end
end
#delegate :get, to: :connection
def get(...)
@last_response = connection.get(...)
end
end
end
end