Reputation: 45941
Consider a Rack app. I only want to handle the error if we are not running a test:
begin
do_something
if ENV[ 'RACK_ENV' ] != 'test'
rescue => error
handle_error error
end
end
end
This generates syntax error, unexpected keyword_rescue (SyntaxError)
rescue => error
Is there a way to do this?
Upvotes: 15
Views: 11567
Reputation: 1
You have to raise the error, or it will change the error to RuntimeError
My solution would be
begin
do_something
rescue => error
handle_error error if ENV['RACK_ENV'] != 'test'
raise error
end
Upvotes: 0
Reputation: 10061
Could you do something like this?
begin
do_something
rescue => error
if ENV["RACK_ENV"] == "test"
raise error
else
handle_error error
end
end
This would re-throw the exception if you are not testing.
EDIT
As @Max points out, you can be a little more succinct with this.
begin
do_something
rescue => error
raise if ENV["RACK_ENV"] == "test"
handle_error error
end
Upvotes: 16
Reputation: 156612
You could always rescue it then then either handle or rethrow depending on your condition
begin
do_something
rescue => error
if ENV['RACK_ENV'] != 'test'
handle_error error
else
raise error
end
end
Upvotes: 3