diff --git a/lib/dfe/analytics/send_events.rb b/lib/dfe/analytics/send_events.rb index 69346af7..50cd1837 100644 --- a/lib/dfe/analytics/send_events.rb +++ b/lib/dfe/analytics/send_events.rb @@ -15,9 +15,25 @@ def perform(events) if DfE::Analytics.log_only? Rails.logger.info("DfE::Analytics: #{events.inspect}") else - DfE::Analytics.events_client.insert(events, ignore_unknown: true) + response = DfE::Analytics.events_client.insert(events, ignore_unknown: true) + raise SendEventsError, response.insert_errors unless response.success? end end end + + class SendEventsError < StandardError + attr_reader :insert_errors + + def initialize(insert_errors) + @insert_errors = insert_errors + + message = insert_errors + .flat_map(&:errors) + .map { |error| error.try(:message) || error['message'] } + .compact.join("\n") + + super("Could not insert all events:\n#{message}") + end + end end end diff --git a/lib/dfe/analytics/testing.rb b/lib/dfe/analytics/testing.rb index 38154415..09db9490 100644 --- a/lib/dfe/analytics/testing.rb +++ b/lib/dfe/analytics/testing.rb @@ -40,8 +40,10 @@ def switch_test_mode(test_mode) end class StubClient + Response = Struct.new(:success?) + def insert(*) - true + Response.new(true) end end diff --git a/lib/dfe/analytics/testing/helpers.rb b/lib/dfe/analytics/testing/helpers.rb index 0381916f..72ce32f2 100644 --- a/lib/dfe/analytics/testing/helpers.rb +++ b/lib/dfe/analytics/testing/helpers.rb @@ -37,6 +37,27 @@ def stub_analytics_event_submission .to_return(status: 200, body: '{}', headers: { 'Content-Type' => 'application/json' }) end + def stub_analytics_event_submission_with_insert_errors + stub_bigquery_auth! + + body = { + insertErrors: [ + { + index: 0, + errors: [ + { + reason: 'error', + message: 'An error.' + } + ] + } + ] + } + + stub_request(:post, /bigquery.googleapis.com/) + .to_return(status: 200, body: body.to_json, headers: { 'Content-Type' => 'application/json' }) + end + def with_analytics_config(options) old_config = DfE::Analytics.config.dup DfE::Analytics.configure do |config| diff --git a/spec/dfe/analytics/send_events_spec.rb b/spec/dfe/analytics/send_events_spec.rb index a4419fb5..26b9ba71 100644 --- a/spec/dfe/analytics/send_events_spec.rb +++ b/spec/dfe/analytics/send_events_spec.rb @@ -30,6 +30,26 @@ end end + context 'when the request is not successful' do + before { stub_analytics_event_submission_with_insert_errors } + + subject(:perform) do + DfE::Analytics::Testing.webmock! do + described_class.new.perform([event.as_json]) + end + end + + it 'raises an exception' do + expect { perform }.to raise_error(DfE::Analytics::SendEventsError, /An error./) + end + + it 'contains the insert errors' do + perform + rescue DfE::Analytics::SendEventsError => e + expect(e.insert_errors).to_not be_empty + end + end + context 'when "log_only" is set' do before do allow(DfE::Analytics).to receive(:log_only?).and_return true