rescue

Alternative to “rescue Exception”

陌路散爱 提交于 2019-12-13 01:13:13
问题 I get some unexpected errors on occasion such as timeout errors, 503 errors, etc. There are errors that I don't even know about that I may receive. I can't account for all of them by doing something like: rescue Timeout::Error => e It's also a terrible idea to rescue Exception . What is an alternative that I could use? I want my code to rescue all of them when there is an error; if there is no error, I need it to be avoided. I want to be able to kill my script but not skip over syntax errors,

Why does this rescue syntax work?

半世苍凉 提交于 2019-12-12 11:06:51
问题 Ok so I have this method of an application I am working with and it works in production. My question why does this work? Is this new Ruby syntax? def edit load_elements(current_user) unless current_user.role?(:admin) respond_to do |format| format.json { render :json => @user } format.xml { render :xml => @user } format.html end rescue ActiveRecord::RecordNotFound respond_to_not_found(:json, :xml, :html) end 回答1: rescue s do not need to be tied to an explicit begin when they're in a method,

Ruby rescue and retry specific code block

谁都会走 提交于 2019-12-10 13:11:36
问题 I have the following code in my script... begin #Loop to create 1000 emails... #Loop to send 1000 emails... rescue Timeout::Error => e retry_attempts += 1 if retry_attempts < 10 retry else puts "Timeout error, deleting emails...".red logs.puts("Rescued a timeout error...#{e}") email_ids_all.each do |email_delete| #delete all email... end My question is what retry is actually going to "retry". If the script has already generated 1000 emails in one loop and sent 999 of them in another loop, and

How to rescue an eval in Ruby?

£可爱£侵袭症+ 提交于 2019-12-08 23:10:58
问题 I'm trying to figure out how to rescue syntax errors that come up when eval() ing code in Ruby 1.8.6. I would expect the following Ruby code: #!/usr/bin/ruby good_str = "(1+1)" bad_str = "(1+1" # syntax error: missing closing paren begin puts eval(good_str) puts eval(bad_str) rescue => exc puts "RESCUED!" end to produce the following result when run: 2 RESCUED! Instead, what I get is: 2 eval_rescue.rb:8: (eval):1: compile error (SyntaxError) (eval):1: syntax error, unexpected $end, expecting

Ruby ignores rescue ArgumentError

☆樱花仙子☆ 提交于 2019-12-07 00:39:51
问题 When I run the following, rescue seems to be ignored for ArgumentError. The ArgumentError error message from Ruby appears on the console, but my puts message does not. I tried rescue with TypeError and ZeroDivisionError, and it worked. def divide(a, b) begin a.to_s + ' divided by ' + b.to_s + ' is ' + (a/b).to_s rescue ArgumentError puts 'there must be two arguments' end end divide(4) 回答1: The exception is not thrown inside the function, but at the point where it is called, so you need to

Rescue Timeout::Error from Redis Gem (Ruby)

安稳与你 提交于 2019-12-06 16:15:20
问题 I need to rescue a Timeout::Error raised from a the Redis library but i'm running into a problem, rescuing that specific class doesn't seem to work. begin Redis.new( { :host => "127.0.0.X" } ) rescue Timeout::Error => ex end => Timeout::Error: Timeout::Error from /Users/me/.rvm/gems/ree-1.8.7-2011.03@gowalla/gems/redis-2.2.0/lib/redis/connection/hiredis.rb:23:in `connect' When i try to rescue Exception it still doesn't work begin Redis.new( { :host => "127.0.0.X" } ) rescue Exception => ex

Rescue_from doesn't rescue Timeout::Error from views or helpers

依然范特西╮ 提交于 2019-12-05 18:38:20
I have an around_filter in my application controller to encase all actions in a timeout block, so that actions fail before hitting the 30 second Heroku limit. I also have a rescue_from Timeout::Error to cleanly rescue these timeouts. Unfortunately, the rescue_from only works some of the time. It works fine if the timeout occurs while executing within the controllers, but fails to rescue if the timeout happens within a view or a helper. Neither Interrupt nor SignalException, both of which Timeout::Error inherits from, rescue correctly either. However, rescuing Exception itself does rescue

Ruby Oneline Rescue

柔情痞子 提交于 2019-12-05 11:08:58
问题 I recently learned that you can use rescue on a line of code in case something goes wrong on that line (see http://www.rubyinside.com/21-ruby-tricks-902.html Tip #21). I have some code that used to look like this: if obj['key'] && obj['key']['key2'] && obj['key']['key2']['name'] name = obj['key']['key2']['name'] else name = '' end With the rescue method, I believe I can change that code into something like this: name = obj['key']['key2']['name'] rescue '' If a nil exception is thrown at any

Ruby ignores rescue ArgumentError

為{幸葍}努か 提交于 2019-12-05 04:29:03
When I run the following, rescue seems to be ignored for ArgumentError. The ArgumentError error message from Ruby appears on the console, but my puts message does not. I tried rescue with TypeError and ZeroDivisionError, and it worked. def divide(a, b) begin a.to_s + ' divided by ' + b.to_s + ' is ' + (a/b).to_s rescue ArgumentError puts 'there must be two arguments' end end divide(4) The exception is not thrown inside the function, but at the point where it is called, so you need to catch it somewhere else: def divide(a, b) a.to_s + ' divided by ' + b.to_s + ' is ' + (a/b).to_s end begin

Rescue Timeout::Error from Redis Gem (Ruby)

夙愿已清 提交于 2019-12-04 20:59:36
I need to rescue a Timeout::Error raised from a the Redis library but i'm running into a problem, rescuing that specific class doesn't seem to work. begin Redis.new( { :host => "127.0.0.X" } ) rescue Timeout::Error => ex end => Timeout::Error: Timeout::Error from /Users/me/.rvm/gems/ree-1.8.7-2011.03@gowalla/gems/redis-2.2.0/lib/redis/connection/hiredis.rb:23:in `connect' When i try to rescue Exception it still doesn't work begin Redis.new( { :host => "127.0.0.X" } ) rescue Exception => ex end => Timeout::Error: Timeout::Error from /Users/me/.rvm/gems/ree-1.8.7-2011.03@gowalla/gems/redis-2.2.0