Skip to content

Commit

Permalink
Rescue gitlab and octokit exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
rubhanazeem committed Sep 13, 2021
1 parent b36fc16 commit b9ef94c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
55 changes: 55 additions & 0 deletions src/api/app/services/scm_exception_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class SCMExceptionHandler
include ActiveSupport::Rescuable
attr_accessor :event_payload, :event_subscription_payload

rescue_from Octokit::AbuseDetected,
Octokit::AccountSuspended,
Octokit::BillingIssue,
Octokit::BranchNotProtected,
Octokit::Conflict,
Octokit::Forbidden,
Octokit::RepositoryUnavailable,
Octokit::NotFound,
Octokit::OneTimePasswordRequired,
Octokit::Unauthorized,
Octokit::UnavailableForLegalReasons,
Octokit::UnsupportedMediaType,
Octokit::CommitIsNotPartOfPullRequest,
Octokit::InstallationSuspended,
Octokit::SAMLProtected,
Octokit::TooLargeContent,
Octokit::TooManyLoginAttempts,
Octokit::UnverifiedEmail,
Octokit::InvalidRepository,
Octokit::PathDiffTooLarge,
Octokit::ServiceUnavailable,
Octokit::InternalServerError do |exception|
# FIXME: Inform users about the exceptions
log(exception)
end

rescue_from Gitlab::Error::Conflict,
Gitlab::Error::Forbidden,
Gitlab::Error::InternalServerError,
Gitlab::Error::MissingCredentials,
Gitlab::Error::NotFound,
Gitlab::Error::ServiceUnavailable,
Gitlab::Error::TooManyRequests,
Gitlab::Error::Unauthorized do |exception|
# FIXME: Inform users about the exceptions
log(exception)
end

def initialize(event_payload, event_subscription_payload, scm_token)
@event_payload = event_payload.deep_symbolize_keys
@event_subscription_payload = event_subscription_payload.deep_symbolize_keys
@scm_token = scm_token
end

private

def log(exception)
token = Token::Workflow.find_by(scm_token: @scm_token)
Rails.logger.error "#{exception.class}: #{exception.message}. TokenID: #{token.id}, User: #{token.user.login}, Event Subscription Payload: #{@event_subscription_payload}"
end
end
10 changes: 5 additions & 5 deletions src/api/app/services/scm_status_reporter.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
class SCMStatusReporter
attr_accessor :event_payload, :event_subscription_payload, :scm_token, :state
class SCMStatusReporter < SCMExceptionHandler
attr_accessor :state

def initialize(event_payload, event_subscription_payload, scm_token, event_type = nil)
@event_payload = event_payload.deep_symbolize_keys
@event_subscription_payload = event_subscription_payload.deep_symbolize_keys
@scm_token = scm_token
super(event_payload, event_subscription_payload, scm_token)

@state = event_type.nil? ? 'pending' : scm_final_state(event_type)
end
Expand All @@ -26,6 +24,8 @@ def call
@state,
status_options)
end
rescue Octokit::Error, Octokit::InvalidRepository, Gitlab::Error::Error => e
rescue_with_handler(e) || raise(e)
end

private
Expand Down
1 change: 1 addition & 0 deletions src/api/config/initializers/zeitwerk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'rss_channel' => 'RSSChannel',
'url_generator' => 'URLGenerator',
'scm_status_reporter' => 'SCMStatusReporter',
'scm_exception_handler' => 'SCMExceptionHandler',
'yaml_to_workflows_service' => 'YAMLToWorkflowsService',
'yaml_downloader' => 'YAMLDownloader'
)
Expand Down

0 comments on commit b9ef94c

Please sign in to comment.