debian-mirror-gitlab/lib/sentry/client.rb

102 lines
2.5 KiB
Ruby
Raw Normal View History

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