Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved error handling and updated tests #250

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading