diff --git a/ee/lib/gitlab/llm/ai_gateway/code_suggestions_client.rb b/ee/lib/gitlab/llm/ai_gateway/code_suggestions_client.rb index a0527e3723d6fdb8686f954b9abebf64d49d0465..18c695f75f52f2019007ad0da59d3c823fea8696 100644 --- a/ee/lib/gitlab/llm/ai_gateway/code_suggestions_client.rb +++ b/ee/lib/gitlab/llm/ai_gateway/code_suggestions_client.rb @@ -31,6 +31,7 @@ def test_completion nil rescue StandardError => err + Gitlab::ErrorTracking.track_exception(err) err.message end diff --git a/ee/lib/gitlab/llm/chain/tools/ci_editor_assistant/executor.rb b/ee/lib/gitlab/llm/chain/tools/ci_editor_assistant/executor.rb index 7426379c38eb7b92cefca703b49b632ad82a9836..02a968f00e306ab77dba60fab3867f48e604b6cd 100644 --- a/ee/lib/gitlab/llm/chain/tools/ci_editor_assistant/executor.rb +++ b/ee/lib/gitlab/llm/chain/tools/ci_editor_assistant/executor.rb @@ -54,7 +54,9 @@ class Executor < Tool def perform(&_block) Answer.new(status: :ok, context: context, content: request, tool: nil) - rescue StandardError + rescue StandardError => e + Gitlab::ErrorTracking.track_exception(e) + Answer.error_answer(context: context, content: _("Unexpected error")) end traceable :perform, run_type: 'tool' diff --git a/ee/lib/gitlab/llm/chain/tools/identifier.rb b/ee/lib/gitlab/llm/chain/tools/identifier.rb index b10458f6ce49ccc194e6438e7c9e16efa15a3114..ef3b27b9dd7df29762648ff6d5fee965425dc4a2 100644 --- a/ee/lib/gitlab/llm/chain/tools/identifier.rb +++ b/ee/lib/gitlab/llm/chain/tools/identifier.rb @@ -44,7 +44,7 @@ def perform(&_block) options[:suggestions] += error_message rescue StandardError => e - logger.error(message: "Error", error: e.message, class: self.class.to_s) + Gitlab::ErrorTracking.track_exception(e) return Answer.error_answer(context: context, content: _("Unexpected error")) end diff --git a/ee/lib/gitlab/llm/chain/tools/slash_command_tool.rb b/ee/lib/gitlab/llm/chain/tools/slash_command_tool.rb index a09e5fcc84e762da8f16e339484f4a5744a60fa7..a07cecc8c87fdc5e32cc90beb8b55b3e1828f31c 100644 --- a/ee/lib/gitlab/llm/chain/tools/slash_command_tool.rb +++ b/ee/lib/gitlab/llm/chain/tools/slash_command_tool.rb @@ -11,7 +11,9 @@ def perform content = request(&streamed_request_handler(StreamedAnswer.new)) Answer.new(status: :ok, context: context, content: content, tool: nil) - rescue StandardError + rescue StandardError => e + Gitlab::ErrorTracking.track_exception(e) + Answer.error_answer(context: context, content: _("Unexpected error")) end traceable :perform, run_type: 'tool' diff --git a/ee/spec/lib/gitlab/llm/ai_gateway/code_suggestions_client_spec.rb b/ee/spec/lib/gitlab/llm/ai_gateway/code_suggestions_client_spec.rb index d9e15edf8426241cf2ad1467d532af34394586be..6e77be94c7b35eddf0f1c2fe5c986faf6577a7c3 100644 --- a/ee/spec/lib/gitlab/llm/ai_gateway/code_suggestions_client_spec.rb +++ b/ee/spec/lib/gitlab/llm/ai_gateway/code_suggestions_client_spec.rb @@ -53,6 +53,12 @@ stub_request(:post, /#{Gitlab::AiGateway.url}/).to_raise(StandardError.new('an error')) end + it 'tracks an exception' do + expect(Gitlab::ErrorTracking).to receive(:track_exception).with(instance_of(StandardError)) + + result + end + it_behaves_like 'error response', 'an error' end end diff --git a/ee/spec/lib/gitlab/llm/chain/tools/ci_editor_assistant/executor_spec.rb b/ee/spec/lib/gitlab/llm/chain/tools/ci_editor_assistant/executor_spec.rb index ac5aa2281b87746f1fc459ad4fbeb552d32cb655..274475a1b2f4f6cf98c3cae876f1a02b880bf281 100644 --- a/ee/spec/lib/gitlab/llm/chain/tools/ci_editor_assistant/executor_spec.rb +++ b/ee/spec/lib/gitlab/llm/chain/tools/ci_editor_assistant/executor_spec.rb @@ -56,6 +56,7 @@ it 'returns error answer' do allow(tool).to receive(:request).and_raise(StandardError) + expect(Gitlab::ErrorTracking).to receive(:track_exception).with(instance_of(StandardError)) expect(tool.execute.content).to eq('Unexpected error') end end diff --git a/ee/spec/lib/gitlab/llm/chain/tools/explain_code/executor_spec.rb b/ee/spec/lib/gitlab/llm/chain/tools/explain_code/executor_spec.rb index 642e054b34991f821e7fb14d5743470476cba54a..f0a2e247189db1adb9b241c35ae2c3637edcad15 100644 --- a/ee/spec/lib/gitlab/llm/chain/tools/explain_code/executor_spec.rb +++ b/ee/spec/lib/gitlab/llm/chain/tools/explain_code/executor_spec.rb @@ -103,6 +103,8 @@ it 'returns error answer' do allow(tool).to receive(:request).and_raise(StandardError) + expect(Gitlab::ErrorTracking).to receive(:track_exception).with(instance_of(StandardError)) + expect(tool.execute.content).to eq('Unexpected error') end end diff --git a/ee/spec/lib/gitlab/llm/chain/tools/refactor_code/executor_spec.rb b/ee/spec/lib/gitlab/llm/chain/tools/refactor_code/executor_spec.rb index 850c04c04658288911e404bb1bcf9c098a6c4ae6..028f2dc99e93ac8cc04404ed514c68e60c16d59c 100644 --- a/ee/spec/lib/gitlab/llm/chain/tools/refactor_code/executor_spec.rb +++ b/ee/spec/lib/gitlab/llm/chain/tools/refactor_code/executor_spec.rb @@ -110,6 +110,8 @@ it 'returns error answer' do allow(tool).to receive(:request).and_raise(StandardError) + expect(Gitlab::ErrorTracking).to receive(:track_exception).with(instance_of(StandardError)) + expect(tool.execute.content).to eq('Unexpected error') end end diff --git a/ee/spec/lib/gitlab/llm/chain/tools/write_tests/executor_spec.rb b/ee/spec/lib/gitlab/llm/chain/tools/write_tests/executor_spec.rb index 964824c39ce58711ca1795281ace322e0852d186..e9cbb27273a18cf1442b88d7b980683b9654ae6d 100644 --- a/ee/spec/lib/gitlab/llm/chain/tools/write_tests/executor_spec.rb +++ b/ee/spec/lib/gitlab/llm/chain/tools/write_tests/executor_spec.rb @@ -94,6 +94,8 @@ it 'returns error answer' do allow(tool).to receive(:request).and_raise(StandardError) + expect(Gitlab::ErrorTracking).to receive(:track_exception).with(instance_of(StandardError)) + expect(tool.execute.content).to eq('Unexpected error') end end diff --git a/lib/gitlab/error_tracking.rb b/lib/gitlab/error_tracking.rb index 0ad2d19be5bb10177a22c7107e6b343401f296ce..a4e481472dc3ec0100f9f083c5d5464e9afe4a12 100644 --- a/lib/gitlab/error_tracking.rb +++ b/lib/gitlab/error_tracking.rb @@ -82,7 +82,8 @@ def track_and_raise_for_dev_exception(exception, extra = {}, tags = {}) raise exception if should_raise_for_dev? end - # This should be used when you only want to track the exception. + # This should be used when you want to track the exception and not raise + # with the default trackers (Sentry and Logger). # # If the exception implements the method `sentry_extra_data` and that method # returns a Hash, then the return value of that method will be merged into @@ -93,7 +94,7 @@ def track_exception(exception, extra = {}, tags = {}) end # This should be used when you only want to log the exception, - # but not send it to Sentry. + # but not send it to Sentry or raise. # # If the exception implements the method `sentry_extra_data` and that method # returns a Hash, then the return value of that method will be merged into