diff --git a/lib/buildkite/test_collector.rb b/lib/buildkite/test_collector.rb index 010175b..535d68e 100644 --- a/lib/buildkite/test_collector.rb +++ b/lib/buildkite/test_collector.rb @@ -101,5 +101,11 @@ def self.enable_tracing! Buildkite::TestCollector::Uploader.tracer&.backfill(:sql, finish - start, **{ query: payload[:sql] }) end end + + def self.safe(&block) + block.call + rescue StandardError => e + logger.error("Buildkite::TestCollector received exception: #{e}") + end end end diff --git a/lib/buildkite/test_collector/library_hooks/minitest.rb b/lib/buildkite/test_collector/library_hooks/minitest.rb index 0a302aa..07aeb4e 100644 --- a/lib/buildkite/test_collector/library_hooks/minitest.rb +++ b/lib/buildkite/test_collector/library_hooks/minitest.rb @@ -12,4 +12,4 @@ class Minitest::Test Buildkite::TestCollector.enable_tracing! -Buildkite::TestCollector::Uploader.configure +Buildkite::TestCollector.safe { Buildkite::TestCollector::Uploader.configure } diff --git a/lib/buildkite/test_collector/library_hooks/rspec.rb b/lib/buildkite/test_collector/library_hooks/rspec.rb index edc25f0..fc385b6 100644 --- a/lib/buildkite/test_collector/library_hooks/rspec.rb +++ b/lib/buildkite/test_collector/library_hooks/rspec.rb @@ -12,7 +12,7 @@ config.before(:suite) do config.add_formatter Buildkite::TestCollector::RSpecPlugin::Reporter - Buildkite::TestCollector::Uploader.configure + Buildkite::TestCollector.safe { Buildkite::TestCollector::Uploader.configure } end config.around(:each) do |example| diff --git a/spec/test_collector_spec.rb b/spec/test_collector_spec.rb index d8318a8..34123f8 100644 --- a/spec/test_collector_spec.rb +++ b/spec/test_collector_spec.rb @@ -24,4 +24,27 @@ expect(analytics.url).to eq "https://analytics-api.buildkite.com/v1/uploads" end end + + describe ".safe" do + let(:logger) { TestLogger.new } + + class TestLogger + attr_reader :errors + + def initialize + @errors = [] + end + + def error(message) + @errors << message + end + end + + before { Buildkite::TestCollector.logger = logger } + + it "suppresses exceptions and logs them to logger.error" do + expect{ described_class.safe { raise "penguines dance" } }.to_not raise_error + expect(logger.errors).to eq(["Buildkite::TestCollector received exception: penguines dance"]) + end + end end