From db353eb79a55a4035fd4553b8d82cfcfd93ecf46 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 24 Jul 2023 23:46:54 +0000 Subject: Add latest changes from gitlab-org/gitlab@16-2-stable-ee --- lib/gitlab_settings/options.rb | 21 ++++++++++++++++++--- spec/lib/gitlab_settings/options_spec.rb | 16 ++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/gitlab_settings/options.rb b/lib/gitlab_settings/options.rb index 68555794436..3338c2d216b 100644 --- a/lib/gitlab_settings/options.rb +++ b/lib/gitlab_settings/options.rb @@ -123,7 +123,7 @@ module GitlabSettings def stringify_keys! error_msg = "Warning: Do not mutate #{self.class} objects: `#{__method__}`" - Gitlab::ErrorTracking.track_and_raise_for_dev_exception(RuntimeError.new(error_msg), method: __method__) + log_and_raise_dev_exception(error_msg, method: __method__) to_hash.deep_stringify_keys end @@ -133,7 +133,7 @@ module GitlabSettings def symbolize_keys! error_msg = "Warning: Do not mutate #{self.class} objects: `#{__method__}`" - Gitlab::ErrorTracking.track_and_raise_for_dev_exception(RuntimeError.new(error_msg), method: __method__) + log_and_raise_dev_exception(error_msg, method: __method__) to_hash.deep_symbolize_keys end @@ -151,7 +151,7 @@ module GitlabSettings if @options.respond_to?(name) error_msg = "Calling a hash method on #{self.class}: `#{name}`" - Gitlab::ErrorTracking.track_and_raise_for_dev_exception(RuntimeError.new(error_msg), method: name) + log_and_raise_dev_exception(error_msg, method: name) return @options.public_send(name, *args, &block) # rubocop: disable GitlabSecurity/PublicSend end @@ -164,5 +164,20 @@ module GitlabSettings @options.respond_to?(name, include_all) end + + private + + # We can't call Gitlab::ErrorTracking.track_and_raise_for_dev_exception + # because that method will attempt to load ApplicationContext and + # fail to load User since the Devise is not yet set up in + # `config/initialiers/8_devise.rb`. + def log_and_raise_dev_exception(message, extra = {}) + raise message unless Rails.env.production? + + # Gitlab::BacktraceCleaner drops config/initializers, so we just limit the + # backtrace to the first 10 lines. + payload = extra.merge(message: message, caller: caller[0..10]) + Gitlab::AppJsonLogger.warn(payload) + end end end diff --git a/spec/lib/gitlab_settings/options_spec.rb b/spec/lib/gitlab_settings/options_spec.rb index abb895032c9..6a53b005025 100644 --- a/spec/lib/gitlab_settings/options_spec.rb +++ b/spec/lib/gitlab_settings/options_spec.rb @@ -12,9 +12,11 @@ RSpec.describe GitlabSettings::Options, :aggregate_failures, feature_category: : it 'returns the unchanged internal hash' do stub_rails_env('production') - expect(Gitlab::ErrorTracking) - .to receive(:track_and_raise_for_dev_exception) - .with(RuntimeError.new("Warning: Do not mutate GitlabSettings::Options objects: `#{method}`"), method: method) + expect(Gitlab::AppJsonLogger) + .to receive(:warn) + .with(hash_including( + message: "Warning: Do not mutate GitlabSettings::Options objects: `#{method}`", + method: method)) .and_call_original expect(options.send(method)).to be_truthy @@ -234,9 +236,11 @@ RSpec.describe GitlabSettings::Options, :aggregate_failures, feature_category: : it 'delegates the method to the internal options hash' do stub_rails_env('production') - expect(Gitlab::ErrorTracking) - .to receive(:track_and_raise_for_dev_exception) - .with(RuntimeError.new('Calling a hash method on GitlabSettings::Options: `delete`'), method: :delete) + expect(Gitlab::AppJsonLogger) + .to receive(:warn) + .with(hash_including( + message: 'Calling a hash method on GitlabSettings::Options: `delete`', + method: :delete)) .and_call_original expect { options.foo.delete('bar') } -- cgit v1.2.3