对于程序异常(或者 404)情况,我进行了捕捉处理,出错时会打出栈信息,development 环境下是 ok 的,但 product 下就不行。 相关代码就是 application_controller 下的:
def self.rescue_errors
unless Rails.application.config.consider_all_requests_local
rescue_from Exception, :with => :render_500
rescue_from RuntimeError, :with => :render_500
rescue_from ActionController::RoutingError, :with => :render_404
rescue_from ActionController::UnknownController, :with => :render_404
rescue_from ActionController::UnknownAction, :with => :render_404
end
end
rescue_errors
def render_500(exception)
logger.error(exception.message + "\n"+exception.backtrace.join("\n"))
render_optional_error_file(500)
end
def render_404(message='')
render_optional_error_file(404,message)
end
def render_optional_error_file(status_code,message='')
@title = "出错了!"
if !message.blank?
@message = message
elsif status_code == 404
@message = "页面不存在."
elsif status_code == 403
@message = "权限被拒绝."
elsif status_code == 500
@message = "系统错误,请稍候光临."
elsif status_code == 503
@message = "系统抢修中,稍候回来."
else
@message = "未知错误,请稍候光临."
end
render :template => "/errors/error",:format => [:html], :handler => [:erb],:status => status_code,:layout => "application"
end
在 product 时页面能正常实现出错提示信息,说明已经调用 render_optional_error_file(500) 了,但它上面的 log 就出不来,我试了打出 info 信息也不行。。。。。
还有个郁闷的情况,也是 product 环境,发现有 2b 请求时框架打出异常了,但提示没搞明白,因为 errors/error模版文件是存在的,是由:formats=>[:png], :handlers=>[:erb, :builder] 这个造成的??。。。。。
Started GET "/cms/uploads/allimg/120209/3866_120209095905_1.png" for 216.24.204.228 at 2012-03-19 12:06:52 +0800
2012-03-19 12:06:52 FATAL --
ActionView::MissingTemplate (Missing template errors/error with {:locale=>[:en], :formats=>[:png], :handlers=>[:erb, :builder]}. Searched in:
* "/home/tuicool/app/lemon/app/views"
):
app/controllers/application_controller.rb:63:in `render_optional_error_file'
app/controllers/application_controller.rb:33:in `render_500'