13得票3回答
与Rails中的救援合作

我正在处理以下代码片段;def index @user = User.find(params[:id]) rescue flash[:notice] = "ERROR" redirect_to(:action => 'index') else fl...

13得票1回答
Elixir:正确打印__STACKTRACE__的方法

我知道在 elixir 的 catch/rescue 块中可以使用 __STACKTRACE__ 获取完整的堆栈跟踪,但正确的打印方式是什么?在我的情况下,我从一个错误中进行了救援,但我仍然希望将其记录到控制台。这是我现在正在做的事情:def unreliable_method(item) d...

12得票2回答
如何在Rails 4中从ActionDispatch::ParamsParser::ParseError错误中恢复

Rails 4新增了异常ActionDispatch::ParamsParser::ParseError,但由于它位于中间件堆栈中,似乎无法在正常的控制器环境中进行救援。在JSON API应用程序中,我希望以标准错误格式响应。 这个Gist展示了一种通过插入中间件来拦截和响应的策略。按照这个...

11得票2回答
当我在一个' rescue '块中使用'retry'时,Ruby不会执行'ensure'块。

考虑这个 begin-rescue-ensure 块:attempts=0 begin make_service_call() rescue Exception retry unless attempts>2 exit -1 ensure attemps += 1 end...

10得票3回答
Ruby Timeout::timeout不能触发异常,也没有返回所记录的内容

我有这段代码:begin complete_results = Timeout.timeout(4) do results = platform.search(artist, album_name) end rescue Timeout::Error puts ...

9得票2回答
Ruby的$!只在rescue块中保留值吗?

begin raise 'foo' rescue puts $!.inspect # => #<RuntimeError: foo> ensure puts $!.inspect # => nil end puts $!.inspect # => ni...

9得票2回答
Ruby忽略了rescue ArgumentError异常处理

当我运行以下代码时,似乎忽略了ArgumentError的rescue处理。Ruby的ArgumentError错误信息出现在控制台上,但我的puts消息没有出现。我尝试使用TypeError和ZeroDivisionError进行rescue处理,它们可以正常工作。 def divide(...

9得票2回答
在Ruby中,当文件无法打开时,最清晰的打开文件并进行“救援”的方法是什么?

我现在正在使用以下代码来解决这个问题 begin File.open(filename, 'r') rescue print "failed to open #{filename}\n" exit end 有没有更简单的方法来做这件事,就像Perl中的'open (IN, $...

8得票1回答
Ruby中如何针对特定的代码块进行异常处理和重试

我在脚本中有以下代码... begin #Loop to create 1000 emails... #Loop to send 1000 emails... rescue Timeout::Error => e retry_attempts += 1...

7得票2回答
Rails 3.1:应用程序如何处理ActiveRecord :: RecordInvalid的不同“原因”(例如,重复与验证错误)?

在我的应用程序中,有时我需要即时创建一个用户,该用户的电子邮件必须是有效的格式,并且必须是唯一的。我希望根据引发错误的验证类型(无效格式或重复)将其重定向到不同的位置。在我的代码中,我有: begin user.save! flash[:notice] = "...