Skip to content

Commit

Permalink
improved error handling and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iulian03 committed Apr 10, 2024
1 parent 46109ce commit 2847d49
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
17 changes: 14 additions & 3 deletions lib/mangopay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,13 @@ def request(method, url, params={}, filters={}, headers_or_idempotency_key = nil
raise MangoPay::ResponseError.new(uri, '408', {'Message' => 'Request Timeout'}) if res.nil?

# decode json data
data = res.body.to_s.empty? ? {} : JSON.load(res.body.to_s)
begin
data = res.body.to_s.empty? ? {} : JSON.load(res.body.to_s)
rescue MultiJson::ParseError
details = {}
details['Message'] = res.body
raise MangoPay::ResponseError.new(uri, res.code, details)
end

unless res.is_a?(Net::HTTPOK)
raise MangoPay::ResponseError.new(uri, res.code, data)
Expand Down Expand Up @@ -298,12 +304,17 @@ def do_request_with_log(http, req, uri)
res
ensure
line = "#{log_severity(res)} #{line}"
if time.nil?
time_log = "[Unknown ms]"
else
time_log = "[#{(time * 1000).round(1)}ms]"
end
if res.nil?
params = ''
line += "\n [#{(time * 1000).round(1)}ms] 408 Request Timeout #{params}\n"
line += "\n #{time_log} 408 Request Timeout #{params}\n"
else
params = FilterParameters.response(res.body)
line += "\n [#{(time * 1000).round(1)}ms] #{res.code} #{params}\n"
line += "\n #{time_log} #{res.code} #{params}\n"
end
logger.info { line }
end
Expand Down
2 changes: 0 additions & 2 deletions spec/mangopay/card_registration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
it 'creates a new card registration' do
created = new_card_registration
expect(created['Id']).not_to be_nil
expect(created['Id'].to_i).to be > 0
expect(created['AccessKey']).not_to be_nil
expect(created['PreregistrationData']).not_to be_nil
expect(created['CardRegistrationURL']).not_to be_nil
Expand Down Expand Up @@ -51,7 +50,6 @@

# card id filled-in...
expect(card_id).not_to be_nil
expect(card_id.to_i).to be > 0

# ...and points to existing (newly created) card
card = MangoPay::Card.fetch(card_id)
Expand Down
1 change: 0 additions & 1 deletion spec/mangopay/preauthorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
cardreg = new_card_registration_completed
created = new_card_preauthorization
expect(created['Id']).not_to be_nil
expect(created['Id'].to_i).to be > 0
expect(created['CardId']).to eq(cardreg['CardId'])
expect(created['AuthorId']).to eq(new_natural_user["Id"])
expect(created['PayInId']).to be_nil
Expand Down
1 change: 0 additions & 1 deletion spec/mangopay/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@
fetched = MangoPay::Card.fetch(card['CardId'])

expect(fetched['Id']).not_to be_nil
expect(fetched['Id'].to_i).to be > 0
expect(fetched['UserId']).to eq(new_natural_user["Id"])
expect(fetched['Currency']).to eq('EUR')
end
Expand Down

0 comments on commit 2847d49

Please sign in to comment.