2019-01-09 16:04:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-04 10:11:19 -05:00
|
|
|
module ErrorTracking
|
|
|
|
class SentryClient
|
|
|
|
include SentryClient::Event
|
|
|
|
include SentryClient::Projects
|
|
|
|
include SentryClient::Issue
|
|
|
|
include SentryClient::Repo
|
|
|
|
include SentryClient::IssueLink
|
2019-12-12 10:08:41 -05:00
|
|
|
|
2019-01-09 16:04:27 -05:00
|
|
|
Error = Class.new(StandardError)
|
2019-03-29 10:53:40 -04:00
|
|
|
MissingKeysError = Class.new(StandardError)
|
2019-01-09 16:04:27 -05:00
|
|
|
|
|
|
|
attr_accessor :url, :token
|
|
|
|
|
|
|
|
def initialize(api_url, token)
|
|
|
|
@url = api_url
|
|
|
|
@token = token
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-01-14 10:07:55 -05:00
|
|
|
def api_urls
|
2021-03-04 10:11:19 -05:00
|
|
|
@api_urls ||= SentryClient::ApiUrls.new(@url)
|
2020-01-14 10:07:55 -05:00
|
|
|
end
|
|
|
|
|
2019-03-29 10:53:40 -04:00
|
|
|
def handle_mapping_exceptions(&block)
|
|
|
|
yield
|
|
|
|
rescue KeyError => e
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking.track_exception(e)
|
2019-11-21 07:06:40 -05:00
|
|
|
raise MissingKeysError, "Sentry API response is missing keys. #{e.message}"
|
2019-03-29 10:53:40 -04:00
|
|
|
end
|
|
|
|
|
2019-01-09 16:04:27 -05:00
|
|
|
def request_params
|
|
|
|
{
|
|
|
|
headers: {
|
2020-01-15 19:08:32 -05:00
|
|
|
'Content-Type' => 'application/json',
|
2019-01-09 16:04:27 -05:00
|
|
|
'Authorization' => "Bearer #{@token}"
|
|
|
|
},
|
|
|
|
follow_redirects: false
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-01-31 05:05:29 -05:00
|
|
|
def http_get(url, params = {})
|
2020-01-06 16:07:43 -05:00
|
|
|
http_request do
|
2019-04-07 03:51:36 -04:00
|
|
|
Gitlab::HTTP.get(url, **request_params.merge(params))
|
|
|
|
end
|
2020-01-06 16:07:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def http_put(url, params = {})
|
|
|
|
http_request do
|
2020-01-15 19:08:32 -05:00
|
|
|
Gitlab::HTTP.put(url, **request_params.merge(body: params.to_json))
|
2020-01-06 16:07:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-22 10:08:48 -05:00
|
|
|
def http_post(url, params = {})
|
|
|
|
http_request do
|
|
|
|
Gitlab::HTTP.post(url, **request_params.merge(body: params.to_json))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-06 16:07:43 -05:00
|
|
|
def http_request
|
|
|
|
response = handle_request_exceptions do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
2019-04-07 03:51:36 -04:00
|
|
|
handle_response(response)
|
2019-01-09 16:04:27 -05:00
|
|
|
end
|
|
|
|
|
2019-04-07 03:51:36 -04:00
|
|
|
def handle_request_exceptions
|
|
|
|
yield
|
2019-08-06 22:42:20 -04:00
|
|
|
rescue Gitlab::HTTP::Error => e
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking.track_exception(e)
|
2019-04-07 03:51:36 -04:00
|
|
|
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'
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking.track_exception(e)
|
2019-04-07 03:51:36 -04:00
|
|
|
raise_error "Sentry request failed due to #{e.class}"
|
|
|
|
end
|
|
|
|
|
2019-01-09 16:04:27 -05:00
|
|
|
def handle_response(response)
|
2020-01-16 13:08:46 -05:00
|
|
|
unless response.code.between?(200, 204)
|
2019-04-07 03:51:36 -04:00
|
|
|
raise_error "Sentry response status code: #{response.code}"
|
2019-01-09 16:04:27 -05:00
|
|
|
end
|
|
|
|
|
2019-12-02 07:06:45 -05:00
|
|
|
{ body: response.parsed_response, headers: response.headers }
|
2019-01-09 16:04:27 -05:00
|
|
|
end
|
|
|
|
|
2019-04-07 03:51:36 -04:00
|
|
|
def raise_error(message)
|
2021-03-04 10:11:19 -05:00
|
|
|
raise SentryClient::Error, message
|
2019-04-07 03:51:36 -04:00
|
|
|
end
|
2019-01-09 16:04:27 -05:00
|
|
|
end
|
|
|
|
end
|