diff --git a/common/lib/dependabot/exception_capturer.rb b/common/lib/dependabot/exception_capturer.rb new file mode 100644 index 000000000000..d0b8354f97f8 --- /dev/null +++ b/common/lib/dependabot/exception_capturer.rb @@ -0,0 +1,34 @@ +# typed: strong +# frozen_string_literal: true + +require "sorbet-runtime" + +module Dependabot + module ExceptionCapturer + extend T::Sig + + # An array of captured exceptions stored for later retrieval + @captured_exceptions = T.let([], T::Array[StandardError]) + + sig { params(error: StandardError).void } + def self.capture_exception(error:) + @captured_exceptions << error + end + + sig { params(block: T.proc.params(error: StandardError).void).void } + def self.handle_captured_exceptions(&block) + @captured_exceptions.each(&block) + clear_captured_exceptions + end + + sig { returns(T::Array[StandardError]) } + def self.captured_exceptions + @captured_exceptions + end + + sig { void } + def self.clear_captured_exceptions + @captured_exceptions = [] + end + end +end diff --git a/common/lib/dependabot/pull_request_creator/message_builder.rb b/common/lib/dependabot/pull_request_creator/message_builder.rb index 521eb88231b3..28a5b0c94633 100644 --- a/common/lib/dependabot/pull_request_creator/message_builder.rb +++ b/common/lib/dependabot/pull_request_creator/message_builder.rb @@ -887,6 +887,7 @@ def package_manager sig { params(method: String, err: StandardError).void } def suppress_error(method, err) + Dependabot::ExceptionCapturer.capture_exception(error: err) Dependabot.logger.error("Error while generating #{method}: #{err.message}") Dependabot.logger.error(err.backtrace&.join("\n")) end diff --git a/updater/bin/update_files.rb b/updater/bin/update_files.rb index a055b598a5c5..f64d47da8b9f 100644 --- a/updater/bin/update_files.rb +++ b/updater/bin/update_files.rb @@ -10,6 +10,7 @@ require "dependabot/service" require "dependabot/setup" require "dependabot/update_files_command" +require "dependabot/exception_capturer" require "debug" if ENV["DEBUG"] flamegraph = ENV.fetch("FLAMEGRAPH", nil) @@ -31,7 +32,12 @@ class UpdaterKilledError < StandardError; end Dependabot::Environment.job_id, Dependabot::Environment.job_token ) - Dependabot::Service.new(client: api_client).capture_exception(error: error, tags: tags) + service = Dependabot::Service.new(client: api_client) + + Dependabot::ExceptionCapturer.handle_captured_exceptions do |exception| + service.capture_exception(error: exception, tags: tags) + end + service.capture_exception(error: error, tags: tags) exit end diff --git a/updater/lib/dependabot/update_files_command.rb b/updater/lib/dependabot/update_files_command.rb index b3cb91d74b93..552c3b8f7be6 100644 --- a/updater/lib/dependabot/update_files_command.rb +++ b/updater/lib/dependabot/update_files_command.rb @@ -51,6 +51,10 @@ def perform_job # successfully processed unless it actually raises. service.mark_job_as_processed(dependency_snapshot.base_commit_sha) end + ensure + Dependabot::ExceptionCapturer.handle_captured_exceptions do |exception| + service.capture_exception(error: exception, job: job) + end end private