Redis Gem(Ruby)中的Rescue Timeout :: Error [英] Rescue Timeout::Error from Redis Gem (Ruby)

查看:161
本文介绍了Redis Gem(Ruby)中的Rescue Timeout :: Error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Redis图书馆中拯救一个 Timeout :: Error ,但是我遇到了一个问题,拯救了这个特定的类似乎没有办法。

  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:在'connect'

当我尝试营救异常它仍然不起作用

  begin 
Redis.new({:host =>127.0。 0.X})
rescue异常=> 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:在'connect'

如果我尝试手动提出异常,我可以救援,但不要知道为什么当我从Redis Gem(2.2.0)内部调用它时,我无法拯救它。

  begin 
raise超时::错误
rescue Timeout :: Error => ex
放弃ex
end

超时::错误
=> nil

任何线索如何解决这个异常?

解决方案

你在irb中运行了这个代码,对吧?你得到的例外实际上并没有被 Redis.new 提出。它正在被检查方法引发,irb调用来显示您刚键入的表达式的值。



只看一下堆栈跟踪(我缩短了路径,使其清晰):

  ruby​​-1.8.7-p330 :009> Redis.new(:host =>google.com)
超时::错误:时间到了!
from /.../SystemTimer-1.2.3/lib/system_timer/concurrent_timer_pool.rb:63:in`trigger_next_expired_timer_at'
from /.../SystemTimer-1.2.3/lib/system_timer/ concurrent_timer_pool.rb:68:在`trigger_next_expired_timer'
from /.../SystemTimer-1.2.3/lib/system_timer.rb:85:in`install_ruby_sigalrm_handler'
from /..../lib /ruby/1.8/monitor.rb:242:in`synchronize'
from /.../SystemTimer-1.2.3/lib/system_timer.rb:83:in`install_ruby_sigalrm_handler'
from /。来自/.../redis-2.2.2/lib/redis/connection/ruby.rb的`call'
../redis-2.2.2/lib/redis/connection/ruby.rb:26:in :26:在`initialize'
from /.../redis-2.2.2/lib/redis/connection/ruby.rb:26:in`new'
from /.../redis -2.2.2 / lib / redis / connection / ruby​​.rb:26:在`connect'
from /.../SystemTimer-1.2.3/lib/system_timer.rb:60:in`timeout_after'
from /.../redis-2.2.2/lib/redis/connection/ruby.rb:115:in`with_timeout'
from /.../redis-2.2.2/li b / redis / connection / ruby​​.rb:25:在`connect'
from /.../redis-2.2.2/lib/redis/client.rb:227:in`establish_connection'
来自/.../redis-2.2.2/lib/redis/client.rb:23:in`connect'
from /.../redis-2.2.2/lib/redis/client.rb: 247:在`ensure_connected'
from /.../redis-2.2.2/lib/redis/client.rb:137:in`process'
... 2级...
from /.../redis-2.2.2/lib/redis/client.rb:46:in`call'
from /.../redis-2.2.2/lib/redis.rb: 90:在`info'
from /..../lib/ruby/1.8/monitor.rb:242:in`synchronize'
from /.../redis-2.2.2/lib /redis.rb:89:in'info'
from /.../redis-2.2.2/lib/redis.rb:1075:in`inspect'
from /..../ lib / ruby​​ / 1.8 / monitor.rb:242:在`synchronize'
from /.../redis-2.2.2/lib/redis.rb:1074:in`inspect'
from / .... / lib / ruby​​ / 1.8 / irb.rb:310:在`output_value'
从/..../lib/ruby/1.8/irb.rb:159:in`eval_input'
from /..../lib/ruby/1.8/irb.rb:271:in` signal_status '
from /..../lib/ruby/1.8/irb.rb:155:in`eval_input'
from /..../lib/ruby/1.8/irb.rb:154 :在`eval_input'
从/..../lib/ruby/1.8/irb.rb:71:in`start'
从/..../lib/ruby/1.8/irb .rb:70:在````
从/..../lib/ruby/1.8/irb.rb:70:in`start'
从/..../bin/irb :17

如上图所示, inspect ,而不是 Redis.new 。当您在Redis对象上调用 inspect ,而不是仅仅打印出它的状态,它实际上做了很多事情。在这种情况下, inspect 尝试连接到服务器,并在超时时引发异常。这对我来说似乎是一个非常糟糕的设计,也许我们应该向Redis宝石的维护者提交错误报告。



这将导致IRB中的一些有趣的行为: / p>


  • 键入 Redis.new(:host =>google.com)导致上述异常

  • 键入 Redis.new(:host =>google.com); 'hello'导致' => 你好'



如果你想抓住这个异常,尝试调用在开始/救援/结束程序段内确保_连接


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/lib/redis/connection/hiredis.rb:23:in `connect'

If i try to raise the exception manually, i can rescue it but don't know why i can't rescue it when it's called from within the Redis Gem (2.2.0).

begin
  raise Timeout::Error
rescue Timeout::Error => ex
  puts ex 
end

Timeout::Error
=> nil 

Any clue how to rescue this exception?

解决方案

You ran this code in irb, right? The exception you are getting is not actually being raised by Redis.new. It is being raised by the inspect method, which irb calls to show you the value of the expression you just typed.

Just look at the stack trace (I shortened the paths to make it legible):

ruby-1.8.7-p330 :009 >   Redis.new(:host => "google.com")
Timeout::Error: time's up!
    from /.../SystemTimer-1.2.3/lib/system_timer/concurrent_timer_pool.rb:63:in `trigger_next_expired_timer_at'
    from /.../SystemTimer-1.2.3/lib/system_timer/concurrent_timer_pool.rb:68:in `trigger_next_expired_timer'
    from /.../SystemTimer-1.2.3/lib/system_timer.rb:85:in `install_ruby_sigalrm_handler'
    from /..../lib/ruby/1.8/monitor.rb:242:in `synchronize'
    from /.../SystemTimer-1.2.3/lib/system_timer.rb:83:in `install_ruby_sigalrm_handler'
    from /.../redis-2.2.2/lib/redis/connection/ruby.rb:26:in `call'
    from /.../redis-2.2.2/lib/redis/connection/ruby.rb:26:in `initialize'
    from /.../redis-2.2.2/lib/redis/connection/ruby.rb:26:in `new'
    from /.../redis-2.2.2/lib/redis/connection/ruby.rb:26:in `connect'
    from /.../SystemTimer-1.2.3/lib/system_timer.rb:60:in `timeout_after'
    from /.../redis-2.2.2/lib/redis/connection/ruby.rb:115:in `with_timeout'
    from /.../redis-2.2.2/lib/redis/connection/ruby.rb:25:in `connect'
    from /.../redis-2.2.2/lib/redis/client.rb:227:in `establish_connection'
    from /.../redis-2.2.2/lib/redis/client.rb:23:in `connect'
    from /.../redis-2.2.2/lib/redis/client.rb:247:in `ensure_connected'
    from /.../redis-2.2.2/lib/redis/client.rb:137:in `process'
... 2 levels...
    from /.../redis-2.2.2/lib/redis/client.rb:46:in `call'
    from /.../redis-2.2.2/lib/redis.rb:90:in `info'
    from /..../lib/ruby/1.8/monitor.rb:242:in `synchronize'
    from /.../redis-2.2.2/lib/redis.rb:89:in `info'
    from /.../redis-2.2.2/lib/redis.rb:1075:in `inspect'
    from /..../lib/ruby/1.8/monitor.rb:242:in `synchronize'
    from /.../redis-2.2.2/lib/redis.rb:1074:in `inspect'
    from /..../lib/ruby/1.8/irb.rb:310:in `output_value'
    from /..../lib/ruby/1.8/irb.rb:159:in `eval_input'
    from /..../lib/ruby/1.8/irb.rb:271:in `signal_status'
    from /..../lib/ruby/1.8/irb.rb:155:in `eval_input'
    from /..../lib/ruby/1.8/irb.rb:154:in `eval_input'
    from /..../lib/ruby/1.8/irb.rb:71:in `start'
    from /..../lib/ruby/1.8/irb.rb:70:in `catch'
    from /..../lib/ruby/1.8/irb.rb:70:in `start'
    from /..../bin/irb:17

As you can see above, the exception occurs inside inspect, not Redis.new. When you call inspect on a Redis object, instead of just printing out its state it actually does a lot of things. In this case, inspect attempts to connect to the server and throws an exception when that times out. This seems like a very bad design to me and maybe we should file a bug report to the maintainers of the Redis gem.

This leads to some interesting behavior in IRB:

  • Typing Redis.new(:host => "google.com") results in an exception as shown above
  • Typing Redis.new(:host => "google.com"); 'hello' results in '=> "hello"'

If you want to catch this exception, try calling ensure_connected inside your begin/rescue/end block.

这篇关于Redis Gem(Ruby)中的Rescue Timeout :: Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆