在Rails 2.1.x中处理RoutingError的最佳方法? [英] Best way to deal with RoutingError in Rails 2.1.x?

查看:80
本文介绍了在Rails 2.1.x中处理RoutingError的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails 2.1中的routing.rb代码,并试图将其处理到我可以对在找不到正确路径时抛出的RoutingError异常做一些有用的事情。 / p>

这是一个棘手的问题,因为有些URL只是普通的BAD:/azenv.php bot攻击,人们输入/ bar / foo /

然后存在一个细微的路由问题,我们希望在此处得到通知:/ artists / , 要么 ///。在这种情况下,我们可能希望抛出一个错误,或者不希望出现一个错误……或者,我们让Google向我们发送了曾经有效但不再因为人们删除它们而不再有用的URL。



在每种情况下,我都希望有一种方法来包含,分析和过滤返回的路径,或者至少要有一种Railsy的方法来管理通过常规后备捕获 URL的路由。



编辑:



所以这里的代码是:

 #文件供应商/rails/actionpack/lib/action_controller/rescue.rb,第141行

def rescue_action_without_handler(exception)
log_error(exception)如果记录器
是否执行了delete_results?

#让异常根据需要更改响应。
#例如,MethodNotAllowed设置Allow标头。
if exception.respond_to?(:handle_response!)
exception.handle_response!(response)
end

if think_all_requests_local || local_request?
rescue_action_locally(例外)
其他
rescue_action_in_public(例外)
结束
结束

所以我们最好的选择是重写log_error(exception),以便我们可以根据异常过滤掉异常。那么在ApplicationController中

  def log_error(exception)
message ='...'
if should_log_exception_as_debug吗? (例外)
logger.debug(消息)
其他
logger.error(消息)
结束
结束

def should_log_exception_as_debug?(
返回(ActionController :: RoutingError ===例外)
结束

在需要其他控制器逻辑,路由等的地方添加逻辑盐。

解决方案

不!!!不要在您的控制器上实现method_missing!



经常被吹捧的模式是添加一条路线:

  map.connect'*',:controller => 错误,:action => 'not_found'

可以在此处显示适当错误的位置。



Rails还具有一种称为rescue_action_in_public的机制,您可以在其中编写自己的错误处理逻辑-我们确实应该清理它并鼓励人们使用它。 PDI! :-)


I'm playing with the routing.rb code in Rails 2.1, and trying to to get it to the point where I can do something useful with the RoutingError exception that is thrown when it can't find the appropriate path.

This is a somewhat tricky problem, because there are some class of URLs which are just plain BAD: the /azenv.php bot attacks, the people typing /bar/foo/baz into the URL, etc... we don't want that.

Then there's subtle routing problems, where we do want to be notified: /artists/ for example, or ///. In these situations, we may want an error being thrown, or not... or we get Google sending us URLs which used to be valid but are no longer because people deleted them.

In each of these situations, I want a way to contain, analyze and filter the path that we get back, or at least some Railsy way to manage routing past the normal 'fallback catchall' url. Does this exist?

EDIT:

So the code here is:

# File vendor/rails/actionpack/lib/action_controller/rescue.rb, line 141

def rescue_action_without_handler(exception)
 log_error(exception) if logger
 erase_results if performed?

 # Let the exception alter the response if it wants.
 # For example, MethodNotAllowed sets the Allow header.
 if exception.respond_to?(:handle_response!)
   exception.handle_response!(response)
 end

 if consider_all_requests_local || local_request?
   rescue_action_locally(exception)
 else
  rescue_action_in_public(exception)
 end
end

So our best option is to override log_error(exception) so that we can filter down the exceptions according to the exception. So in ApplicationController

def log_error(exception)
    message = '...'
    if should_log_exception_as_debug?(exception)
      logger.debug(message)
    else
      logger.error(message)
    end
end

def should_log_exception_as_debug?(exception)
   return (ActionController::RoutingError === exception)
end

Salt for additional logic where we want different controller logic, routes, etc.

解决方案

Nooooo!!! Don't implement method_missing on your controller! And please try to avoid action_missing as well.

The frequently touted pattern is to add a route:

map.connect '*', :controller => 'error', :action => 'not_found'

Where you can show an appropriate error.

Rails also has a mechanism called rescue_action_in_public where you can write your own error handling logic -- we really should clean it up and encourage people to use it. PDI! :-)

这篇关于在Rails 2.1.x中处理RoutingError的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆