Skip to content

Commit

Permalink
Catch SocketError
Browse files Browse the repository at this point in the history
We catch GdsApi::BaseError for scenarios when external services to the app
aren't available we keep the app being somewhat usable. Since SocketError
isn't handled by GDS API Adapters it means that this error can block someone
using the application.

Since GDS API Adapters wraps a variety of network related exceptions already
then it makes sense that it includes this one for consistency.
  • Loading branch information
MuriloDalRi committed Dec 20, 2018
1 parent 58d7e4d commit 1698084
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/gds_api/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def raven_context
class EndpointNotFound < BaseError; end
class TimedOutException < BaseError; end
class InvalidUrl < BaseError; end
class SocketErrorException < BaseError; end

# Superclass for all 4XX and 5XX errors
class HTTPErrorResponse < BaseError
Expand Down
3 changes: 3 additions & 0 deletions lib/gds_api/json_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ def do_request(method, url, params = nil, additional_headers = {})
rescue Errno::ECONNRESET => e
logger.error loggable.merge(status: 'connection_reset', error_message: e.message, error_class: e.class.name, end_time: Time.now.to_f).to_json
raise GdsApi::TimedOutException.new
rescue SocketError => e
logger.error loggable.merge(status: 'socket_error', error_message: e.message, error_class: e.class.name, end_time: Time.now.to_f).to_json
raise GdsApi::SocketErrorException.new
end
end
end
8 changes: 8 additions & 0 deletions test/json_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def test_post_requests_timeout
end
end

def test_socket_error
url = "http://some.endpoint/some.json"
stub_request(:get, url).to_raise(SocketError)
assert_raises GdsApi::SocketErrorException do
@client.get_json(url)
end
end

def test_get_should_raise_error_on_restclient_error
url = "http://some.endpoint/some.json"
stub_request(:get, url).to_raise(RestClient::ServerBrokeConnection)
Expand Down

0 comments on commit 1698084

Please sign in to comment.