Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move rescued types to a constant #69

Merged
merged 1 commit into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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