From 3463724750c96e4f7d24d58b2d34bd1650df239e Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Tue, 17 Sep 2019 10:25:54 -0500 Subject: [PATCH 1/3] Add failing example illustrating behavior of MismatchError in bare rescue --- test/scientist/experiment_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/scientist/experiment_test.rb b/test/scientist/experiment_test.rb index e12ef37..8154c58 100644 --- a/test/scientist/experiment_test.rb +++ b/test/scientist/experiment_test.rb @@ -444,6 +444,18 @@ def @ex.raised(op, exception) assert_raises(Scientist::Experiment::MismatchError) { @ex.run } end + it "allows MismatchError to bubble up through bare rescues" do + Fake.raise_on_mismatches = true + @ex.use { "control" } + @ex.try { "candidate" } + runner = -> do + @ex.run + rescue + # StandardError handled + end + assert_raises(Scientist::Experiment::MismatchError) { runner.call } + end + describe "#raise_on_mismatches?" do it "raises when there is a mismatch if the experiment instance's raise on mismatches is enabled" do Fake.raise_on_mismatches = false From 0b76afccfda17ce927522225903c1b0a559d8888 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Mon, 16 Sep 2019 15:57:37 -0500 Subject: [PATCH 2/3] Make MismatchError a base Exception This allows mismatches to avoid being caught by bare rescue's which should help make them more visible in the face of code which chooses to use such a general rescue strategy. --- lib/scientist/experiment.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/scientist/experiment.rb b/lib/scientist/experiment.rb index 53f2ae0..e824268 100644 --- a/lib/scientist/experiment.rb +++ b/lib/scientist/experiment.rb @@ -18,7 +18,7 @@ def self.new(name) end # A mismatch, raised when raise_on_mismatches is enabled. - class MismatchError < StandardError + class MismatchError < Exception attr_reader :name, :result def initialize(name, result) From ad48edc8dd6678ae2e48a624c2c01a5d64dea77d Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Tue, 17 Sep 2019 10:31:34 -0500 Subject: [PATCH 3/3] Fix begin/rescue syntax for older rubies --- test/scientist/experiment_test.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/scientist/experiment_test.rb b/test/scientist/experiment_test.rb index 8154c58..7588286 100644 --- a/test/scientist/experiment_test.rb +++ b/test/scientist/experiment_test.rb @@ -448,11 +448,13 @@ def @ex.raised(op, exception) Fake.raise_on_mismatches = true @ex.use { "control" } @ex.try { "candidate" } - runner = -> do - @ex.run - rescue - # StandardError handled - end + runner = -> { + begin + @ex.run + rescue + # StandardError handled + end + } assert_raises(Scientist::Experiment::MismatchError) { runner.call } end