Skip to content

Commit

Permalink
add more generic error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
liztownd committed Nov 14, 2024
1 parent ec1f5d6 commit 6a4de91
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion modules/travel_pay/app/services/travel_pay/claims_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ def get_claims_by_date_range(params = {}) # rubocop:disable Metrics/MethodLength
'message' => e.message,
'success' => false
}, status: 400)
rescue => e
rescue Common::Exceptions::BackendServiceException => e
Rails.logger.error(message: "#{e}, #{e.original_body}")
Faraday::Response.new(response_body: e.original_body, status: e.original_status)
rescue => e
Faraday::Response.new(response_body: {
'statusCode' => 520, # Unknown error code
'message' => "An unknown error occored: #{e}",
'success' => false
}, status: 520)
end

def get_claim_by_id(claim_id)
Expand Down
14 changes: 14 additions & 0 deletions modules/travel_pay/spec/services/claims_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@
expect(bad_claims_call.body['message']).to include(/Both start and end/i)
end

it 'handles random, unknown errors' do
allow_any_instance_of(TravelPay::ClaimsClient)
.to receive(:get_claims_by_date)
.and_raise(NameError.new('Uninitialized constant.', 'new_constant'))

bad_claims_call = @service.get_claims_by_date_range(
{ 'start_date' => '2024-01-01T16:45:34Z', 'end_date' => '2024-01-01T16:45:34Z' }
)
expect(bad_claims_call.status).to equal(520)
expect(bad_claims_call.body['statusCode']).to equal(520)
expect(bad_claims_call.body['success']).to eq(false)
expect(bad_claims_call.body['message']).to include(/Uninitialized constant/i)
end

it 'returns 400 with error message if dates are invalid' do
allow_any_instance_of(TravelPay::ClaimsClient)
.to receive(:get_claims_by_date)
Expand Down

0 comments on commit 6a4de91

Please sign in to comment.