取消ajax请求会导致rails抛出异常

时间:2012-05-06 03:51:48

标签: ruby-on-rails ruby ajax exception autocomplete

我是铁杆新手,我整天都在寻找答案。免于从乞讨开始。

UI:我有自动完成功能(确切地说,页面上有YUI自动完成功能)。

Y.one('input.search-from').plug(Y.Plugin.AutoComplete, {      
    resultHighlighter: 'phraseMatch',
    source: "/api/open_maps/search/{query}", 
    resultTextLocator: 'display_name'
}

其中“/ api / open_maps / search / {query}”转到rails,在我的情况下被雇用作为代理来解决ajax的相同原始策略。

def search
  uri = URI.parse(searchUrl(CGI.escape(params[:query])))
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)
  render :json => response.body
end

如果我快速键入除了最后一个从autocompleter到rails的ajax请求被取消,这是有道理的,因为我们不再需要它们了。问题是rails为所有被取消的请求抛出了丑陋的例外:

Started GET "/api/open_maps/search/Den" for 127.0.0.1 at Sun May 06    04:20:03 +0100 2012
[2012-05-06 04:20:03] ERROR Errno::EINVAL: Invalid argument
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpresponse.rb:324:in `write'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpresponse.rb:324:in `<<'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpresponse.rb:324:in `_write_data'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpresponse.rb:296:in `send_body_string'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpresponse.rb:187:in `send_body'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpresponse.rb:104:in `send_response'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:79:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/Library/Ruby/Gems/1.8/gems/rack-1.4.1/lib/rack/handler/webrick.rb:13:in `run'
/Library/Ruby/Gems/1.8/gems/rack-1.4.1/lib/rack/server.rb:265:in `start'
/Library/Ruby/Gems/1.8/gems/railties-3.2.3/lib/rails/commands/server.rb:70:in `start'
/Library/Ruby/Gems/1.8/gems/railties-3.2.3/lib/rails/commands.rb:55
/Library/Ruby/Gems/1.8/gems/railties-3.2.3/lib/rails/commands.rb:50:in `tap'
/Library/Ruby/Gems/1.8/gems/railties-3.2.3/lib/rails/commands.rb:50
script/rails:6:in `require'
script/rails:6

另外,看起来ruby正在后台取消请求(这会减慢一切)。

我的问题是:

  1. 我们如何在rails中检测到客户端连接失败/被取消?
  2. 一旦我们知道#1,我们如何取消Net :: HTTP发出的请求?
  3. 我试过开始 - &gt;救援 - &gt;结束块至少压缩异常,但它没有帮助。

1 个答案:

答案 0 :(得分:1)

如果查看跟踪,则错误不会来自Net :: HTTP,这就是rescue尝试没有帮助的原因。当webrick尝试将机架响应写回浏览器时,会引发错误。

您可以通过安装mongrelthinunicorn作为您的网络服务器来解决您的问题。 Webrick内置于Ruby,但众所周知是不健全。