2019-11-08 16:06:38 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ErrorTracking
|
2022-03-16 08:07:24 -04:00
|
|
|
class BaseService < ::BaseProjectService
|
|
|
|
include Gitlab::Utils::UsageData
|
|
|
|
|
|
|
|
def initialize(project, user = nil, params = {})
|
|
|
|
super(project: project, current_user: user, params: params.dup)
|
|
|
|
end
|
|
|
|
|
2019-11-08 16:06:38 -05:00
|
|
|
def execute
|
|
|
|
return unauthorized if unauthorized
|
|
|
|
|
2020-01-28 16:08:56 -05:00
|
|
|
perform
|
2019-11-08 16:06:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-01-21 04:08:43 -05:00
|
|
|
def perform
|
2019-11-08 16:06:38 -05:00
|
|
|
raise NotImplementedError,
|
|
|
|
"#{self.class} does not implement #{__method__}"
|
|
|
|
end
|
|
|
|
|
2020-01-28 16:08:56 -05:00
|
|
|
def compose_response(response, &block)
|
|
|
|
errors = parse_errors(response)
|
|
|
|
return errors if errors
|
|
|
|
|
2022-08-03 08:11:38 -04:00
|
|
|
yield if block
|
2020-01-28 16:08:56 -05:00
|
|
|
|
2022-03-16 08:07:24 -04:00
|
|
|
track_usage_event(params[:tracking_event], current_user.id) if params[:tracking_event]
|
|
|
|
|
2020-01-28 16:08:56 -05:00
|
|
|
success(parse_response(response))
|
|
|
|
end
|
|
|
|
|
2019-11-08 16:06:38 -05:00
|
|
|
def parse_response(response)
|
|
|
|
raise NotImplementedError,
|
|
|
|
"#{self.class} does not implement #{__method__}"
|
|
|
|
end
|
|
|
|
|
2020-01-28 16:08:56 -05:00
|
|
|
def unauthorized
|
2019-11-08 16:06:38 -05:00
|
|
|
return error('Error Tracking is not enabled') unless enabled?
|
|
|
|
return error('Access denied', :unauthorized) unless can_read?
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_errors(response)
|
|
|
|
return error('Not ready. Try again later', :no_content) unless response
|
|
|
|
return error(response[:error], http_status_for(response[:error_type])) if response[:error].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def http_status_for(error_type)
|
|
|
|
case error_type
|
|
|
|
when ErrorTracking::ProjectErrorTrackingSetting::SENTRY_API_ERROR_TYPE_MISSING_KEYS
|
|
|
|
:internal_server_error
|
|
|
|
else
|
|
|
|
:bad_request
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_error_tracking_setting
|
|
|
|
project.error_tracking_setting
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled?
|
|
|
|
project_error_tracking_setting&.enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_read?
|
|
|
|
can?(current_user, :read_sentry_issue, project)
|
|
|
|
end
|
2020-01-21 04:08:43 -05:00
|
|
|
|
|
|
|
def can_update?
|
|
|
|
can?(current_user, :update_sentry_issue, project)
|
|
|
|
end
|
2022-05-11 17:08:09 -04:00
|
|
|
|
|
|
|
def error_repository
|
|
|
|
Gitlab::ErrorTracking::ErrorRepository.build(project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_error_repository_exceptions
|
|
|
|
yield
|
|
|
|
rescue Gitlab::ErrorTracking::ErrorRepository::DatabaseError => e
|
|
|
|
{ error: e.message }
|
|
|
|
end
|
2019-11-08 16:06:38 -05:00
|
|
|
end
|
|
|
|
end
|