Skip to content

Commit

Permalink
Merge pull request #306 from barthez/better-error-handling-in-strateg…
Browse files Browse the repository at this point in the history
…y-wrap

Better error handling in Chewy::Strategy#wrap
  • Loading branch information
pyromaniac committed Jan 6, 2016
2 parents fa46035 + dea0a5d commit 721b2db
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/chewy/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def pop
end

def wrap name
push name
stack = push(name)
yield
ensure
pop
pop if stack
end

private
Expand All @@ -72,7 +72,11 @@ def debug string
end

def resolve name
"Chewy::Strategy::#{name.to_s.camelize}".constantize or raise "Can't find update strategy `#{name}`"
"Chewy::Strategy::#{name.to_s.camelize}".safe_constantize or raise "Can't find update strategy `#{name}`"
rescue NameError => ex
# WORKAROUND: Strange behavior of `safe_constantize` with mongoid gem
raise "Can't find update strategy `#{name}`" if ex.name.to_s.demodulize == name.to_s.camelize
raise
end
end
end
14 changes: 13 additions & 1 deletion spec/chewy/strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
end

describe '#push' do
specify { expect { strategy.push(:unexistant) }.to raise_error(NameError).with_message(/uninitialized constant.*Unexistant/) }
specify { expect { strategy.push(:unexistant) }.to raise_error(RuntimeError).with_message("Can't find update strategy `unexistant`") }

specify do
expect { strategy.push(:atomic) }
Expand All @@ -34,6 +34,18 @@
end
end

describe '#wrap' do
specify { expect { strategy.wrap(:unexistant) {} }.to raise_error(RuntimeError).with_message("Can't find update strategy `unexistant`") }

specify do
expect do
strategy.wrap(:urgent) do
expect(strategy.current).to be_a(Chewy::Strategy::Urgent)
end
end.not_to change { strategy.current }
end
end

context 'nesting', :orm do
before do
stub_model(:city) do
Expand Down

0 comments on commit 721b2db

Please sign in to comment.