【问题标题】:HTTP error handlingHTTP 错误处理
【发布时间】:2018-06-20 16:55:05
【问题描述】:

这是我在这里的第一篇文章,所以我希望我在正确的地方发布这个问题。否则,请让我知道,以便我下次在这里发帖时知道:)

我正在开发一个 RoR 网站,并希望单独处理服务器错误(400、404、500 等)。此外,由于网站是动态的,我想在 rails 环境而不是服务器级别处理错误。 我想做的一个例子是,当用户碰到一个不会加载或根本不存在的页面或模板时,向用户展示可选材料或搜索​​栏。

所以,我做了一些阅读,我认为使用 rescue_from 异常处理程序是我的方法。 (如果你们中的任何人有不同的意见,我们会很高兴听到)。

我有一个简单的工作原型(请参阅下面的代码)启动并运行,但是,当我在代码中包含以下异常处理程序时出现错误:

rescue_from ActionController::MissingTemplate,          :with => :not_found #404

现在,我看不到我有拼写错误,而且我在网上发布的代码中看到了这一行。但是,当我包含它时,会出现以下路由错误:

Routing Error No route matches "/errorhandle" with {:method=>:get}

我正在开发 Rails 2.3.5,也许这与它有关?

我希望你能帮助我阐明这个问题。

干杯! /玛雅

class ApplicationController < ActionController::Base

    helper :all # include all helpers, all the time

    protect_from_forgery #See ActionController::RequestForgeryProtection for details

    #ActiveRecord exceptions
    rescue_from ActiveRecord::RecordNotFound, :with => :not_found #400   

    #ActiveResource exceptions  
    rescue_from ActiveResource::ResourceNotFound, :with => :not_found #404

    #ActionView exceptions
    rescue_from ActionView::TemplateError, :with => :not_found #500

    #ActionController exceptions
    rescue_from ActionController::RoutingError, :with => :not_found #404   

    rescue_from ActionController::UnknownController, :with => :not_found #404 

    rescue_from ActionController::MethodNotAllowed, :with => :not_found #405   

    rescue_from ActionController::InvalidAuthenticityToken, :with => :not_found #405

    rescue_from ActionController::UnknownAction, :with => :not_found #501

    # This particular exception causes all the rest to fail.... why?
    # rescue_from ActionController::MissingTemplate, :with => :not_found #404

    protected
    def not_found
        render :text => "Error", :status => 404
    end

    # Scrub sensitive parameters from your log
    # filter_parameter_logging :password 
end

【问题讨论】:

  • 你能告诉我们来自 routes.rb 的相关路线,如果有的话?
  • 当然Trrevoke,感谢您的快速回复。我没有在 routes.rb 中添加任何东西,所以我想这几乎是标准。文件:ActionController::Routing::Routes.draw 做 |map| map.resources :errorhandlers # ... # 很多行被注释掉了 # ... map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' end
  • 呃,不知道如何让评论看起来美观整洁uark。希望它无论如何都可以阅读......
  • 编辑您的问题,让路线看起来整洁有序
  • Veger:现在似乎无法编辑,但我会在下一篇文章中研究如何编辑。干杯。

标签: ruby-on-rails web-applications error-handling http-headers


【解决方案1】:

快速浏览以下内容: http://www.ruby-forum.com/topic/47898

http://henrik.nyh.se/2008/09/404-invalid-rails-format

特别是第一个链接中的帖子:

您不能使用常规的“救援”关键字来救援 MissingTemplate 例外。

改用rescue_action,例如:

def rescue_action(exception)
  if ::ActionController::MissingTemplate === exception
     render :text => 'rescued'
  else
     super
  end
end

肯特。

【讨论】:

  • 非常感谢 Kent 的回复和链接,它们都非常有帮助。我将阅读 RoR 中的 :: 和一般异常处理,以更好地理解您的回复。 ...如果你不介意的话,我可能会再问几个问题?现在 - 感谢你们所有人,很高兴成为如此活跃的论坛的一部分! /玛雅
  • 我不是肯特 - 肯特是在第一个链接中回答该问题的人 :-) 不客气。无论如何,提出更多问题,这就是这个网站的目的! (当然,请先阅读文档)。
猜你喜欢
  • 2016-09-29
  • 2016-08-06
  • 2012-11-16
  • 2014-08-29
  • 2016-07-12
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多