如何在Rails环境中通过Rack引发URI :: InvalidURIError?

时间:2013-10-31 17:58:09

标签: ruby-on-rails rack

我们的Rails应用有时会收到一个完全乱码的网址,导致Rack引发URI::InvalidURIError。然后,我们的异常通知系统(HoneyBadger)会收到此错误的通知。

但是,我不想知道这些错误:我更喜欢Rack只记录它们(如果可能的话)并吞下它们。

应用程序通过此ActionController::RoutingError

处理routes.rb
# Default route, because these would normally not go through the application rescue_from block
match('*path', to: 'application#routing_error') unless Rails.env.development?

...然后在ApplicationController#routing_error发生路由错误时记录(但不通知)。

但是,这个无效的URI错误发生在路由看到URL之前。

以下是导致问题的示例网址(当然,域名已更改) - http://www.whatever.com/HD2EU]。然后是堆栈跟踪:

[PROJECT_ROOT]/vendor/ruby-1.9.3/lib/ruby/1.9.1/uri/common.rb:176:in `split`
[PROJECT_ROOT]/vendor/ruby-1.9.3/lib/ruby/1.9.1/uri/common.rb:211:in `parse`
[PROJECT_ROOT]/vendor/ruby-1.9.3/lib/ruby/1.9.1/uri/common.rb:747:in `parse`
[PROJECT_ROOT]/vendor/ruby-1.9.3/lib/ruby/1.9.1/uri/common.rb:994:in `URI`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/rack-ssl-1.3.3/lib/rack/ssl.rb:50:in `redirect_to_https`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/rack-ssl-1.3.3/lib/rack/ssl.rb:32:in `call`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:136:in `forward`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:245:in `fetch`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:185:in `lookup`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:66:in `call!`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/rack-cache-1.2/lib/rack/cache/context.rb:51:in `call`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/engine.rb:479:in `call`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/application.rb:223:in `call`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:552:in `process_client`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:632:in `worker_loop`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/newrelic_rpm-3.6.5.130/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb:22:in `call`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/newrelic_rpm-3.6.5.130/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb:22:in `block (4 levels) in <top (required)>`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:500:in `spawn_missing_workers`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:142:in `start`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/gems/unicorn-4.6.3/bin/unicorn:126:in `<top (required)>`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/bin/unicorn:23:in `load`
[PROJECT_ROOT]/vendor/bundle/ruby/1.9.1/bin/unicorn:23:in `<main>`

1 个答案:

答案 0 :(得分:1)

我最后将其添加到honeybadger.rb初始化程序中:

config.ignore << URI::InvalidURIError