Ruby on rails Can';t捕获ActiveRecord::RecordNotFound with rescue

Ruby on rails Can';t捕获ActiveRecord::RecordNotFound with rescue,ruby-on-rails,ruby,rails-activerecord,Ruby On Rails,Ruby,Rails Activerecord,我是Ruby新手,如果这是一个愚蠢的问题,或者如果我没有遵循最佳实践,请容忍我 我正在使用find()在数据库中查找一个对象,并希望它在id的对象不存在的情况下抛出RecordNotFound,如下所示 begin event = Event.find(event_id) rescue ActiveRecord::RecordNotFound => e Rails.logger.debug "Event does not exist, id: " + event_i

我是Ruby新手,如果这是一个愚蠢的问题,或者如果我没有遵循最佳实践,请容忍我

我正在使用
find()
在数据库中查找一个对象,并希望它在id的对象不存在的情况下抛出
RecordNotFound
,如下所示

  begin
    event = Event.find(event_id)
  rescue ActiveRecord::RecordNotFound => e
    Rails.logger.debug "Event does not exist, id: " + event_id
    return {
        # return "unauthorized" to avoid testing existence of event id
        # (some redacted codes)
    }
  end 
但不知何故,它没有被捕获(rescue块中的日志没有打印),整个程序只返回内部服务器错误。以下是堆栈跟踪:

Completed 500 Internal Server Error in 22ms (ActiveRecord: 1.0ms)



ActiveRecord::RecordNotFound (Couldn't find Event with 'id'=999):

lib/sync/create_update_event_handler.rb:78:in `handleRequest'
app/controllers/sync_controller.rb:36:in `block in sync'
app/controllers/sync_controller.rb:31:in `each'
app/controllers/sync_controller.rb:31:in `sync'
  Rendering /usr/local/rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /usr/local/rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (6.4ms)
  Rendering /usr/local/rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms)
  Rendering /usr/local/rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.9ms)
  Rendered /usr/local/rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (36.6ms)
我唯一能想到的是有两个不同的ActiveRecord::RecordNotFound,我抓错了一个,但我不知道是否是这样,也不知道如何验证它。 我做错了什么

======================================

更新

问题是在rescue块中,我将
event\u id
(一个整数)连接到一个字符串。
确实捕获了
RecordNotFound
异常,但是当在rescue块中抛出类型错误时,打印了错误的错误消息。

如果这样做,您不会得到错误

event=event.find_by(id:event_id)

在这种情况下,如果无法通过ID找到记录,它将只
event==nil
为nil。 在这种情况下,如果无法通过ID找到记录,它将只
event==nil
为nil

你粘贴的代码对我来说很好。如果在日志中看不到输出,请检查您的环境和日志级别设置信息、警告、调试等。500错误表示某种控制器操作引发了错误

要确保您的救援模块正在执行,请尝试执行日志以外的操作。如果您正在运行开发服务器,可以尝试:

    begin
         event = Event.find(event_id)
     rescue ActiveRecord::RecordNotFound => e
         msg = "Event does not exist, id: #{event_id.to_s}"
         Rails.logger.debug msg.
         puts msg 
         binding.pry # if you have gem 'pry' in your development gems.
         File.open('test.log', 'w') {|f| f.write msg} #check if this appears in root of your app
         return {
                 # return "unauthorized" to avoid testing existence of event id
                 # (some redacted codes)
         }
     end 

更新:我根据你的答案更改了字符串插值。您还可以在插值中调用
.to_s
,而不是结束引号和追加。

@event=event.find(params[:id])
。您应该改为编写
params[:id]
。这就是错误的原因。

结果显示错误消息是错误的

问题是我将
事件id
(一个整数)转换为字符串。 但是Rails以某种方式打印出RecordNotFound异常

这个问题可以通过更换来解决
Rails.logger.debug”事件不存在,id:“+Event\u id
具有
Rails.logger.debug”事件不存在,id:“+Event\u id.to\u s


感谢@lacostenycoder提醒我注意错误消息。

我不确定您的问题,但这些链接可能会对您有所帮助。另外,请阅读Rails的
rescue\u from
功能,该功能允许您在控制器本身上为特定异常定义模板。您确定
ActiveRecord::RecordNotFound
event=event.find(event\u id)上引发错误吗
line?@MartinZinovsky我很确定,因为浏览器上显示的堆栈跟踪突出显示了该行。我觉得很奇怪,我显式地捕获了那个异常,但它仍然会导致一个内部服务器错误。@user3374124您能在您的问题中发布完整的操作代码吗?在那一行
lib/sync/create\u update\u event\u handler.rb:78
没有引起那个错误的东西吗?服务器日志呢<代码>Rails.logger.debug应该打印出来,以防错误被挽救。您还可以将
return
更改为
render
,如果它的控制器操作感谢您的想法。你的建议让我注意到了救援区块的内容,在那里我发现了问题,仅供参考。我没有否决你的回复。谢谢你的想法。你的建议让我注意到了救援区块的内容,我在那里发现了问题。问题是错误消息,我将
事件id
(整数)连接到一个字符串。仅供参考,我没有否决你的回复。谢谢,这是我使用的解决方法。但是我试图找出我的代码出了什么问题,而不是避免异常。很高兴我能提供帮助。我更新了我的答案。谢谢