方法1:http://www.uedidea.com/rails%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86.html
def add_to_cart
product = Product.find(params[:id])
rescue ActiveRecord::RecordNotFound #拦截Product.find()异常
logger.error("Attempt to access invalid product #{params[:id]}") #日志记录
flash[:notice] = "Invalid product" #notice 存放提示信息
redirect_to :action => "index" #跳转到index
end
begin
@user.destroy
flash[:notice] = "User #{@user.name} deleted"
rescue Exception => e #如果上面的代码执行发生异常就捕获
flash[:notice] = e.message
end
begin
raise ArgumentError, "Bad data"
rescue => err
puts err
ensure
... #执行清理工作
end
方法2:around_filter
http://hlee.iteye.com/blog/323025
around_filter :rescue_record_not_found
def rescue_record_not_found
begin
yield
rescue ActiveRecord::RecordNotFound
render :file => "#{RAILS_ROOT}/public/404.html"
end
end
hlee 在文中还提到可以这样做:
rescue_from ActiveRecord::RecordNotFound, with => :rescue_record_not_found
def rescue_record_not_found
render :file => "#{RAILS_ROOT}/public/404.html"
end
方法3:rescue_action_in_public
参考:http://202.199.224.30/post/article/7
def rescue_action_in_public(exception)
logger.error("rescue_action_in_public executed")
case exception
when ActiveRecord::RecordNotFound, ::ActionController::RoutingError,
::ActionController::UnknownAction
logger.error("404 displayed")
render(:file => "#{RAILS_ROOT}/public/404.html",
:status => "404 Not Found")
else
logger.error("500 displayed")
render(:file => "#{RAILS_ROOT}/public/500.html",
:status => "500 Error")
# SystemNotifier.deliver_exception_notification(self, request,
# exception)
end
end
注意:在不同环境中的配置,生产环境中,默认的配置应该就可以显示效果,但在开发模式下,需要确认/config/environments/development.rb中的
代码
config.action_controller.consider_all_requests_local = false
如果在本机访问必须增加(app/controllers/application.rb):
代码
def local_request?
false
end
错误类型参考:
DEFAULT_RESCUE_RESPONSE = :internal_server_error
DEFAULT_RESCUE_RESPONSES = {
'ActionController::RoutingError' => :not_found,
'ActionController::UnknownAction' => :not_found,
'ActiveRecord::RecordNotFound' => :not_found,
'ActiveRecord::StaleObjectError' => :conflict,
'ActiveRecord::RecordInvalid' => :unprocessable_entity,
'ActiveRecord::RecordNotSaved' => :unprocessable_entity,
'ActionController::MethodNotAllowed' => :method_not_allowed,
'ActionController::NotImplemented' => :not_implemented,
'ActionController::InvalidAuthenticityToken' => :unprocessable_entity
}
DEFAULT_RESCUE_TEMPLATE = 'diagnostics'
DEFAULT_RESCUE_TEMPLATES = {
'ActionView::MissingTemplate' => 'missing_template',
'ActionController::RoutingError' => 'routing_error',
'ActionController::UnknownAction' => 'unknown_action',
'ActionView::TemplateError' => 'template_error'
}
又看了这篇贴子中的讨论:http://www.iteye.com/topic/708334
rails的错误处理还需要再研究。
分享到:
相关推荐
Rails异常处理程序 从版本1升级? 这是Rails的灵活异常处理程序,适用于希望创建自己的错误跟踪服务的人员。 它面向经验丰富的Rails开发人员,这些开发人员不仅要管理几个Rails应用程序,而且还要管理多个应用程序。...
Exception Notification - Rails的异常通知插件。能够在Rack/Rails应用发生错误时发送通知。
由于Web浏览器(Rails是Web框架)只能解释,因此必须将Ruby / Rails异常转换为浏览器可以读取的内容。 这是通过调用上述中间件来完成的。 - 如突出显示的那样,HTTP响应的构建独立于Rails堆栈。 这包括分配HTTP...
Rescue-Dog在引发Rails异常时会响应HTTP状态(代码和消息)。 安装 将“救援犬” gem添加到您的Gemfile gem 'rescue-dog' 并运行bundle install命令。 声明CRUD动作 包括Rescue::Controller (Rescue :: ...
允许在后台运行操作提供操作异常/错误的监视和管理。 允许构建分层的同步和异步过程。 允许将流程实现拆分为操作隔离逻辑。 允许在不同流程中重用操作。 每个操作都在其自己的事务中运行,并具有单独的上下文。...
这个gem为提供了简单的错误处理。强调每个字段的错误处理。 与Rails类似, rescue_from块中的逻辑。 由祖先捕获异常,例如CustomError和rescue_from StandardError 。 每个架构配置。 没有依赖关系。用法定义...
无需为您的应用编写自定义错误处理逻辑-Temescal会为您处理! 入门 将gem添加到您的Gemfile中,然后运行bundle install : gem 'temescal' 由于Temescal只是Rack中间件,因此将其添加到您的应用程序非常容易。 ...
Ruby的Bugsnag异常报告程序Ruby的Bugsnag异常报告程序可立即通知您从Rails,Sinatra,Rack或纯Ruby...功能自动报告未处理的异常和崩溃报告已处理的异常附加用户信息以确定崩溃影响了多少人发送自定义诊断数据跟踪事件
异常猎人 Exception Hunter是一个Rails引擎,旨在跟踪您的Rails项目中的错误。 它通过使用Postgres数据库来保存错误及其相应的元数据(例如发生故障时的回溯或环境数据)来工作。 为此,我们会挂接到您应用程序的...
如果您使用异常报告服务(例如Bugsnag),则可能需要定义一个错误处理程序。 有关更多信息,请参见。 主动工作依赖 维护任务框架依赖于后台的ActiveJob来运行任务。 ActiveJob的默认排队后端是。 强烈建议将其更改...
Bugsnag错误监控和Ruby异常报告器 Ruby的Bugsnag异常报告器会立即通知您从 , Sinatra , Rack或纯Ruby应用程序引发的异常。 任何未捕获的异常都会触发通知,该通知将发送到您的Bugsnag项目。产品特点自动报告未处理...
为代理安装 gem 以处理应用程序中未捕获的异常。 安装(Rails 3) 步骤 1.将它作为 gem 添加到Gemfile 中 gem 'runtimeerror_notifier' Step 2.执行以下命令生成config/initializers/runtimeerror_notifier.rb ...
客户端库:用于不同系统(Ruby,Ruby on Rails,带有Objective-C的Cocoa等)的客户端库在发生错误时捕获并记录错误,然后将其发送到API端点。 客户端库可以在下找到。 前端:此网站显示错误信息,并帮助工程师找到...
气刹介绍是一个在线工具,可以在... 该gem中可用的集成列表包括: (作为) Web框架Rails [] Sinatra [] 机架应用[] 作业处理库ActiveJob [] 刷新[] Sidekiq [] DelayedJob [] Shoryuken [] 运动鞋[] 其他图书馆Action
后处理CSS 添加CSS预处理器(Sass,Less等) 添加图像,字体和文件 使用public文件夹 更改HTML 在模块系统之外添加资产 何时使用public文件夹 使用全局变量 添加引导程序使用自定义主题 增加流量 添加路由器 添加...
以运行时异常的方式来管理错误的响应 使用泛型来做强类型编程 多协议扩展支持(REST, RPC, SOAP, etc) Rails3消息队列系统 Sidekiq Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 ...
以运行时异常的方式来管理错误的响应 使用泛型来做强类型编程 多协议扩展支持(REST, RPC, SOAP, etc) Rails3消息队列系统 Sidekiq Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 ...
以运行时异常的方式来管理错误的响应 使用泛型来做强类型编程 多协议扩展支持(REST, RPC, SOAP, etc) Rails3消息队列系统 Sidekiq Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 ...
以运行时异常的方式来管理错误的响应 使用泛型来做强类型编程 多协议扩展支持(REST, RPC, SOAP, etc) Rails3消息队列系统 Sidekiq Sidekiq 为 Rails 3 应用程序提供一个高效的消息队列系统。 Java文件上传组件 ...