Skip to content

Commit

Permalink
OTWO-7177 Return null object on Fisbot Api timeouts (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-sig authored Jan 30, 2024
1 parent 827f5fb commit 2356a91
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
9 changes: 6 additions & 3 deletions app/lib/fisbot/fisbot_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ def find(id)
uri = api_access.resource_uri(id)
response = Net::HTTP.get_response(uri)
handle_errors(response) { new(JSON.parse(response.body)) }
rescue Errno::ECONNREFUSED, Timeout::Error => e
rescue Errno::ECONNREFUSED, Timeout::Error, JSON::ParserError => e
Airbrake.notify(e)
rescue JSON::ParserError
handle_errors(response) { nil }
null_object
end

def all(params)
Expand Down Expand Up @@ -98,6 +97,10 @@ def api_access
ApiAccess.new(resource)
end

def null_object
"Nil#{name}".constantize.new
end

def build_objects(response)
return [] if response.is_a?(Net::HTTPNoContent)

Expand Down
25 changes: 25 additions & 0 deletions test/lib/fisbot_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,29 @@
_(code_location).must_be :valid?
end
end

describe 'find' do
it 'must handle timeout errors' do
Net::HTTP.stubs(:get_response).raises(Timeout::Error)
code_location = CodeLocation.find(42)

_(code_location.scm_type).must_equal :git
_(code_location).must_be_kind_of NilCodeLocation
end

it 'must handle connection refused errors' do
Net::HTTP.stubs(:get_response).raises(Errno::ECONNREFUSED)
code_location = CodeLocation.find(42)

_(code_location.url).must_be :blank?
_(code_location).must_be_kind_of NilCodeLocation
end

it 'must handle JSON parser errors' do
Net::HTTP.stubs(:get_response).returns(stub(body: 'bad JSON response'))
code_location = CodeLocation.find(42)

_(code_location).must_be_kind_of NilCodeLocation
end
end
end

0 comments on commit 2356a91

Please sign in to comment.