From bbb17ea1ea619664b362a9c984da45b940c412a2 Mon Sep 17 00:00:00 2001 From: Peter Leitzen Date: Sun, 7 Apr 2019 07:51:36 +0000 Subject: Handle possible HTTP exception for Sentry client Prior this commit exceptions raised during a HTTP request weren't caught by the Sentry client and were passed to the user. In addition the Sentry client tried to catch a non-existent error `Sentry::Client::SentryError`. Now, the Sentry client catches all possible errors coming from a HTTP request. --- lib/sentry/client.rb | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'lib/sentry') diff --git a/lib/sentry/client.rb b/lib/sentry/client.rb index bb1aa2a7a10..4022e8ff946 100644 --- a/lib/sentry/client.rb +++ b/lib/sentry/client.rb @@ -47,9 +47,11 @@ module Sentry end def http_get(url, params = {}) - resp = Gitlab::HTTP.get(url, **request_params.merge(params)) + response = handle_request_exceptions do + Gitlab::HTTP.get(url, **request_params.merge(params)) + end - handle_response(resp) + handle_response(response) end def get_issues(issue_status:, limit:) @@ -63,14 +65,36 @@ module Sentry http_get(projects_api_url) end + def handle_request_exceptions + yield + rescue HTTParty::Error => e + Gitlab::Sentry.track_acceptable_exception(e) + raise_error 'Error when connecting to Sentry' + rescue Net::OpenTimeout + raise_error 'Connection to Sentry timed out' + rescue SocketError + raise_error 'Received SocketError when trying to connect to Sentry' + rescue OpenSSL::SSL::SSLError + raise_error 'Sentry returned invalid SSL data' + rescue Errno::ECONNREFUSED + raise_error 'Connection refused' + rescue => e + Gitlab::Sentry.track_acceptable_exception(e) + raise_error "Sentry request failed due to #{e.class}" + end + def handle_response(response) unless response.code == 200 - raise Client::Error, "Sentry response status code: #{response.code}" + raise_error "Sentry response status code: #{response.code}" end response end + def raise_error(message) + raise Client::Error, message + end + def projects_api_url projects_url = URI(@url) projects_url.path = '/api/0/projects/' -- cgit v1.2.3