Can salvage be used with a conditional?

Consider the Rack app. I just want to handle the error if we do not run the 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 any way to do this?

+6
source share
2 answers

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 will cause the exception to be thrown again if you are not testing.

EDIT

As @Max points out, you can be a little more concise about this.

 begin do_something rescue => error raise if ENV["RACK_ENV"] == "test" handle_error error end 
+7
source

You can always save it, and then either process it or transfer it depending on your condition.

 begin do_something rescue => error if ENV['RACK_ENV'] != 'test' handle_error error else raise error end end 
+1
source

Source: https://habr.com/ru/post/984330/


All Articles