Skip to content

Commit

Permalink
Merge pull request #69 from github/configurable-rescues
Browse files Browse the repository at this point in the history
Move rescued types to a constant
  • Loading branch information
jbarnette authored Aug 22, 2017
2 parents ee51cd1 + aad9381 commit 042cd55
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,18 @@ Scientist will raise a `Scientist::Experiment::MismatchError` exception if any o

### Handling errors

If an exception is raised within any of scientist's internal helpers, like `publish`, `compare`, or `clean`, the `raised` method is called with the symbol name of the internal operation that failed and the exception that was raised. The default behavior of `Scientist::Default` is to simply re-raise the exception. Since this halts the experiment entirely, it's often a better idea to handle this error and continue so the experiment as a whole isn't canceled entirely:
#### In candidate code

Scientist rescues and tracks _all_ exceptions raised in a `try` or `use` block, including some where rescuing may cause unexpected behavior (like `SystemExit` or `ScriptError`). To rescue a more restrictive set of exceptions, modify the `RESCUES` list:

```ruby
# default is [Exception]
Scientist::Observation::RESCUES.replace [StandardError]
```

#### In a Scientist callback

If an exception is raised within any of Scientist's internal helpers, like `publish`, `compare`, or `clean`, the `raised` method is called with the symbol name of the internal operation that failed and the exception that was raised. The default behavior of `Scientist::Default` is to simply re-raise the exception. Since this halts the experiment entirely, it's often a better idea to handle this error and continue so the experiment as a whole isn't canceled entirely:

```ruby
class MyExperiment
Expand Down
6 changes: 5 additions & 1 deletion lib/scientist/observation.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# What happened when this named behavior was executed? Immutable.
class Scientist::Observation

# An Array of Exception types to rescue when initializing an observation.
# NOTE: This Array will change to `[StandardError]` in the next major release.
RESCUES = [Exception]

# The experiment this observation is for
attr_reader :experiment

Expand All @@ -26,7 +30,7 @@ def initialize(name, experiment, &block)

begin
@value = block.call
rescue Object => e
rescue *RESCUES => e
@exception = e
end

Expand Down
31 changes: 31 additions & 0 deletions test/scientist/observation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,37 @@
assert_nil ob.value
end

describe "::RESCUES" do
before do
@original = Scientist::Observation::RESCUES.dup
end

after do
Scientist::Observation::RESCUES.replace(@original)
end

it "includes all exception types by default" do
ob = Scientist::Observation.new("test", @experiment) do
raise Exception.new("not a StandardError")
end

assert ob.raised?
assert_instance_of Exception, ob.exception
end

it "can customize rescued types" do
Scientist::Observation::RESCUES.replace [StandardError]

ex = assert_raises Exception do
Scientist::Observation.new("test", @experiment) do
raise Exception.new("not a StandardError")
end
end

assert_equal "not a StandardError", ex.message
end
end

it "compares values" do
a = Scientist::Observation.new("test", @experiment) { 1 }
b = Scientist::Observation.new("test", @experiment) { 1 }
Expand Down

0 comments on commit 042cd55

Please sign in to comment.