我是Ruby新手,如果这是个愚蠢的问题,或者我没有遵循最佳实践,请耐心等待我。
我正在使用find()在DB中找到一个对象,并期望它在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 但不知何故,它没有被捕获(救援块中的日志没有被打印出来),整个程序只是返回内部服务器错误。下面是堆栈跟踪:
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,而且我抓错了一个,但是我不知道是否是这种情况,也不知道如何验证它。我做错什么了?
======================================
更新
问题是在拯救块中,我将event_id (一个整数)连接到一个字符串。确实捕获了RecordNotFound异常,但是当类型错误抛出在救援块中时,会打印错误消息。
发布于 2018-03-19 11:24:47
原来错误信息是错误的。
问题是,我将event_id (一个整数)集中到一个字符串。但是Rails以某种方式输出了RecordNotFound异常。
通过将Rails.logger.debug "Event does not exist, id: " + event_id替换为Rails.logger.debug "Event does not exist, id: " + event_id.to_s解决了这个问题。
谢谢你让我注意到错误信息。
发布于 2018-03-18 16:52:07
如果你这样做了,你就不会犯错误
event = Event.find_by(id: event_id)在这种情况下,如果无法通过ID找到记录,则只需event == nil为零。在这种情况下,如果无法通过ID找到记录,则只需event == nil为零。
你粘贴的代码对我来说很好。如果您没有在日志中看到输出,请检查您的环境和日志级别设置信息、警告、调试等。500错误指示某种控制器操作会导致错误。
请参阅Set logging levels in Ruby on Rails
为了确保您的救援块正在执行,请尝试做一些除了日志之外的事情。如果您正在运行开发服务器,您可以尝试:
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内插,而不是关闭引号和附加。
发布于 2018-03-19 07:44:54
@event = Event.find(params[:id])。你应该写params[:id],这是错误的原因。
https://stackoverflow.com/questions/49350120
复制相似问题