Commit ac6367aa authored by Luis Mejia's avatar Luis Mejia Committed by Alper Akgun

Raise exception for StandardError fallback

parent 9d52d643
...@@ -32,6 +32,7 @@ module Gitlab ...@@ -32,6 +32,7 @@ module Gitlab
def value def value
return ::License.trial_ends_on if license_attribute == "trial_ends_on" return ::License.trial_ends_on if license_attribute == "trial_ends_on"
return ::License.current.restricted_user_count if license_attribute == "user_count"
alt_usage_data(fallback: nil) do alt_usage_data(fallback: nil) do
# license_attribute is checked in the constructor, so it's safe # license_attribute is checked in the constructor, so it's safe
......
...@@ -169,7 +169,8 @@ module Gitlab ...@@ -169,7 +169,8 @@ module Gitlab
return -1 if args.any?(&:negative?) return -1 if args.any?(&:negative?)
args.sum args.sum
rescue StandardError rescue StandardError => error
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(error)
FALLBACK FALLBACK
end end
...@@ -179,7 +180,8 @@ module Gitlab ...@@ -179,7 +180,8 @@ module Gitlab
else else
value value
end end
rescue StandardError rescue StandardError => error
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(error)
fallback fallback
end end
...@@ -295,13 +297,15 @@ module Gitlab ...@@ -295,13 +297,15 @@ module Gitlab
def redis_usage_counter def redis_usage_counter
yield yield
rescue ::Redis::CommandError, Gitlab::UsageDataCounters::BaseCounter::UnknownEvent, Gitlab::UsageDataCounters::HLLRedisCounter::EventError rescue ::Redis::CommandError, Gitlab::UsageDataCounters::BaseCounter::UnknownEvent, Gitlab::UsageDataCounters::HLLRedisCounter::EventError => error
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(error)
FALLBACK FALLBACK
end end
def redis_usage_data_totals(counter) def redis_usage_data_totals(counter)
counter.totals counter.totals
rescue ::Redis::CommandError, Gitlab::UsageDataCounters::BaseCounter::UnknownEvent rescue ::Redis::CommandError, Gitlab::UsageDataCounters::BaseCounter::UnknownEvent => error
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(error)
counter.fallback_totals counter.fallback_totals
end end
end end
......
...@@ -17,9 +17,25 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::GenericMetric do ...@@ -17,9 +17,25 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::GenericMetric do
end end
context 'when raising an exception' do context 'when raising an exception' do
it 'return the custom fallback' do before do
allow(Gitlab::ErrorTracking).to receive(:should_raise_for_dev?).and_return(should_raise_for_dev)
expect(ApplicationRecord.database).to receive(:version).and_raise('Error') expect(ApplicationRecord.database).to receive(:version).and_raise('Error')
expect(subject.value).to eq(custom_fallback) end
context 'with should_raise_for_dev? false' do
let(:should_raise_for_dev) { false }
it 'return the custom fallback' do
expect(subject.value).to eq(custom_fallback)
end
end
context 'with should_raise_for_dev? true' do
let(:should_raise_for_dev) { true }
it 'raises an error' do
expect { subject.value }.to raise_error('Error')
end
end end
end end
end end
...@@ -38,9 +54,25 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::GenericMetric do ...@@ -38,9 +54,25 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::GenericMetric do
end end
context 'when raising an exception' do context 'when raising an exception' do
it 'return the default fallback' do before do
allow(Gitlab::ErrorTracking).to receive(:should_raise_for_dev?).and_return(should_raise_for_dev)
expect(ApplicationRecord.database).to receive(:version).and_raise('Error') expect(ApplicationRecord.database).to receive(:version).and_raise('Error')
expect(subject.value).to eq(described_class::FALLBACK) end
context 'with should_raise_for_dev? false' do
let(:should_raise_for_dev) { false }
it 'return the default fallback' do
expect(subject.value).to eq(described_class::FALLBACK)
end
end
context 'with should_raise_for_dev? true' do
let(:should_raise_for_dev) { true }
it 'raises an error' do
expect { subject.value }.to raise_error('Error')
end
end end
end end
end end
......
...@@ -925,10 +925,25 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do ...@@ -925,10 +925,25 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
end end
context 'when retrieve component setting meets exception' do context 'when retrieve component setting meets exception' do
it 'returns -1 for component enable status' do before do
allow(Gitlab::ErrorTracking).to receive(:should_raise_for_dev?).and_return(should_raise_for_dev)
allow(Settings).to receive(:[]).with(component).and_raise(StandardError) allow(Settings).to receive(:[]).with(component).and_raise(StandardError)
end
context 'with should_raise_for_dev? false' do
let(:should_raise_for_dev) { false }
it 'returns -1 for component enable status' do
expect(subject).to eq({ enabled: -1 })
end
end
expect(subject).to eq({ enabled: -1 }) context 'with should_raise_for_dev? true' do
let(:should_raise_for_dev) { true }
it 'raises an error' do
expect { subject.value }.to raise_error(StandardError)
end
end end
end end
end end
......
...@@ -5,11 +5,13 @@ require 'spec_helper' ...@@ -5,11 +5,13 @@ require 'spec_helper'
RSpec.describe Gitlab::Utils::UsageData do RSpec.describe Gitlab::Utils::UsageData do
include Database::DatabaseHelpers include Database::DatabaseHelpers
shared_examples 'failing hardening method' do shared_examples 'failing hardening method' do |raised_exception|
let(:exception) { raised_exception || ActiveRecord::StatementInvalid }
before do before do
allow(Gitlab::ErrorTracking).to receive(:should_raise_for_dev?).and_return(should_raise_for_dev) allow(Gitlab::ErrorTracking).to receive(:should_raise_for_dev?).and_return(should_raise_for_dev)
stub_const("Gitlab::Utils::UsageData::FALLBACK", fallback) stub_const("Gitlab::Utils::UsageData::FALLBACK", fallback)
allow(failing_class).to receive(failing_method).and_raise(ActiveRecord::StatementInvalid) allow(failing_class).to receive(failing_method).and_raise(exception) unless failing_class.nil?
end end
context 'with should_raise_for_dev? false' do context 'with should_raise_for_dev? false' do
...@@ -24,7 +26,7 @@ RSpec.describe Gitlab::Utils::UsageData do ...@@ -24,7 +26,7 @@ RSpec.describe Gitlab::Utils::UsageData do
let(:should_raise_for_dev) { true } let(:should_raise_for_dev) { true }
it 'raises an error' do it 'raises an error' do
expect { subject }.to raise_error(ActiveRecord::StatementInvalid) expect { subject }.to raise_error(exception)
end end
end end
end end
...@@ -366,8 +368,13 @@ RSpec.describe Gitlab::Utils::UsageData do ...@@ -366,8 +368,13 @@ RSpec.describe Gitlab::Utils::UsageData do
expect(described_class.add).to eq(0) expect(described_class.add).to eq(0)
end end
it 'returns the fallback value when adding fails' do context 'when adding fails' do
expect(described_class.add(nil, 3)).to eq(-1) subject { described_class.add(nil, 3) }
let(:fallback) { -1 }
let(:failing_class) { nil }
it_behaves_like 'failing hardening method', StandardError
end end
it 'returns the fallback value one of the arguments is negative' do it 'returns the fallback value one of the arguments is negative' do
...@@ -376,8 +383,13 @@ RSpec.describe Gitlab::Utils::UsageData do ...@@ -376,8 +383,13 @@ RSpec.describe Gitlab::Utils::UsageData do
end end
describe '#alt_usage_data' do describe '#alt_usage_data' do
it 'returns the fallback when it gets an error' do context 'when method fails' do
expect(described_class.alt_usage_data { raise StandardError } ).to eq(-1) subject { described_class.alt_usage_data { raise StandardError } }
let(:fallback) { -1 }
let(:failing_class) { nil }
it_behaves_like 'failing hardening method', StandardError
end end
it 'returns the evaluated block when give' do it 'returns the evaluated block when give' do
...@@ -391,14 +403,22 @@ RSpec.describe Gitlab::Utils::UsageData do ...@@ -391,14 +403,22 @@ RSpec.describe Gitlab::Utils::UsageData do
describe '#redis_usage_data' do describe '#redis_usage_data' do
context 'with block given' do context 'with block given' do
it 'returns the fallback when it gets an error' do context 'when method fails' do
expect(described_class.redis_usage_data { raise ::Redis::CommandError } ).to eq(-1) subject { described_class.redis_usage_data { raise ::Redis::CommandError } }
let(:fallback) { -1 }
let(:failing_class) { nil }
it_behaves_like 'failing hardening method', ::Redis::CommandError
end end
it 'returns the fallback when Redis HLL raises any error' do context 'when Redis HLL raises any error' do
stub_const("Gitlab::Utils::UsageData::FALLBACK", 15) subject { described_class.redis_usage_data { raise Gitlab::UsageDataCounters::HLLRedisCounter::CategoryMismatch } }
let(:fallback) { 15 }
let(:failing_class) { nil }
expect(described_class.redis_usage_data { raise Gitlab::UsageDataCounters::HLLRedisCounter::CategoryMismatch } ).to eq(15) it_behaves_like 'failing hardening method', Gitlab::UsageDataCounters::HLLRedisCounter::CategoryMismatch
end end
it 'returns the evaluated block when given' do it 'returns the evaluated block when given' do
...@@ -407,9 +427,14 @@ RSpec.describe Gitlab::Utils::UsageData do ...@@ -407,9 +427,14 @@ RSpec.describe Gitlab::Utils::UsageData do
end end
context 'with counter given' do context 'with counter given' do
it 'returns the falback values for all counter keys when it gets an error' do context 'when gets an error' do
allow(::Gitlab::UsageDataCounters::WikiPageCounter).to receive(:totals).and_raise(::Redis::CommandError) subject { described_class.redis_usage_data(::Gitlab::UsageDataCounters::WikiPageCounter) }
expect(described_class.redis_usage_data(::Gitlab::UsageDataCounters::WikiPageCounter)).to eql(::Gitlab::UsageDataCounters::WikiPageCounter.fallback_totals)
let(:fallback) { ::Gitlab::UsageDataCounters::WikiPageCounter.fallback_totals }
let(:failing_class) { ::Gitlab::UsageDataCounters::WikiPageCounter }
let(:failing_method) { :totals }
it_behaves_like 'failing hardening method', ::Redis::CommandError
end end
it 'returns the totals when couter is given' do it 'returns the totals when couter is given' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment