Skip to content

Commit

Permalink
Fail gracefully when config file is missing or bad
Browse files Browse the repository at this point in the history
This is better than raising obscure exceptions from the internals.
  • Loading branch information
iangreenleaf committed Jan 19, 2013
1 parent b040fcd commit 8840ccb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/split/experiment.rb
Expand Up @@ -157,6 +157,7 @@ def self.load_alternatives_for(name)

def self.load_alternatives_from_configuration_for(name)
alts = Split.configuration.experiment_for(name)[:variants]
raise ArgumentError, "Experiment configuration is missing :variants array" if alts.nil?
if alts.is_a?(Hash)
alts.keys
else
Expand Down
15 changes: 15 additions & 0 deletions spec/helper_spec.rb
Expand Up @@ -649,6 +649,21 @@ def should_finish_experiment(experiment_name, should_finish=true)
names_and_weights.should == [['control_opt', 0.18], ['second_opt', 0.18], ['third_opt', 0.64]]
names_and_weights.inject(0){|sum, nw| sum + nw[1]}.should == 1.0
end

it "fails gracefully if config is missing experiment" do
Split.configuration.experiments = { :other_experiment => { :foo => "Bar" } }
lambda { ab_test :my_experiment }.should raise_error(/not found/i)
end

it "fails gracefully if config is missing" do
Split.configuration.experiments = nil
lambda { ab_test :my_experiment }.should raise_error(/not found/i)
end

it "fails gracefully if config is missing variants" do
Split.configuration.experiments[:my_experiment] = { :foo => "Bar" }
lambda { ab_test :my_experiment }.should raise_error(/variants/i)
end
end

it 'should handle multiple experiments correctly' do
Expand Down

0 comments on commit 8840ccb

Please sign in to comment.