diff options
Diffstat (limited to 'lib/sentry/client.rb')
-rw-r--r-- | lib/sentry/client.rb | 64 |
1 files changed, 49 insertions, 15 deletions
diff --git a/lib/sentry/client.rb b/lib/sentry/client.rb index 49ec196b103..4022e8ff946 100644 --- a/lib/sentry/client.rb +++ b/lib/sentry/client.rb @@ -3,7 +3,7 @@ module Sentry class Client Error = Class.new(StandardError) - SentryError = Class.new(StandardError) + MissingKeysError = Class.new(StandardError) attr_accessor :url, :token @@ -14,18 +14,29 @@ module Sentry def list_issues(issue_status:, limit:) issues = get_issues(issue_status: issue_status, limit: limit) - map_to_errors(issues) + + handle_mapping_exceptions do + map_to_errors(issues) + end end def list_projects projects = get_projects - map_to_projects(projects) - rescue KeyError => e - raise Client::SentryError, "Sentry API response is missing keys. #{e.message}" + + handle_mapping_exceptions do + map_to_projects(projects) + end end private + def handle_mapping_exceptions(&block) + yield + rescue KeyError => e + Gitlab::Sentry.track_acceptable_exception(e) + raise Client::MissingKeysError, "Sentry API response is missing keys. #{e.message}" + end + def request_params { headers: { @@ -36,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:) @@ -52,12 +65,34 @@ 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.as_json + response + end + + def raise_error(message) + raise Client::Error, message end def projects_api_url @@ -94,7 +129,6 @@ module Sentry def map_to_error(issue) id = issue.fetch('id') - project = issue.fetch('project') count = issue.fetch('count', nil) @@ -117,9 +151,9 @@ module Sentry short_id: issue.fetch('shortId', nil), status: issue.fetch('status', nil), frequency: frequency, - project_id: project.fetch('id'), - project_name: project.fetch('name', nil), - project_slug: project.fetch('slug', nil) + project_id: issue.dig('project', 'id'), + project_name: issue.dig('project', 'name'), + project_slug: issue.dig('project', 'slug') ) end @@ -127,12 +161,12 @@ module Sentry organization = project.fetch('organization') Gitlab::ErrorTracking::Project.new( - id: project.fetch('id'), + id: project.fetch('id', nil), name: project.fetch('name'), slug: project.fetch('slug'), status: project.dig('status'), organization_name: organization.fetch('name'), - organization_id: organization.fetch('id'), + organization_id: organization.fetch('id', nil), organization_slug: organization.fetch('slug') ) end |