Permalink
Browse files

Reraise exceptions from the server thread

  • Loading branch information...
1 parent 43c70e4 commit e3248b7946e38fd291c9c3cce0c481beede37f02 @jferris jferris committed Jan 27, 2012
Showing with 66 additions and 3 deletions.
  1. +32 −3 lib/capybara/driver/webkit.rb
  2. +34 −0 spec/driver_spec.rb
@@ -15,10 +15,8 @@ class WebkitNoResponseError < StandardError
class NodeNotAttachedError < Capybara::ElementNotFound
end
- attr_reader :browser
-
def initialize(app, options={})
- @app = app
+ @app = ErrorTrapper.new(app)
@options = options
@rack_server = Capybara::Server.new(@app)
@rack_server.boot if Capybara.run_server
@@ -100,6 +98,7 @@ def wait_until(*args)
end
def reset!
+ @app.reset
browser.reset!
end
@@ -122,10 +121,40 @@ def cookies
@cookie_jar ||= CookieJar.new(browser)
end
+ def browser
+ @app.raise_exception_from_server_thread
+ @browser
+ end
+
private
def url(path)
@rack_server.url(path)
end
+
+ class ErrorTrapper
+ def initialize(app)
+ @app = app
+ end
+
+ def raise_exception_from_server_thread
+ if @exception
+ raise @exception
+ end
+ end
+
+ def reset
+ @exception = nil
+ end
+
+ def call(env)
+ begin
+ @app.call(env)
+ rescue Exception => exception
+ @exception = exception
+ raise exception
+ end
+ end
+ end
end
View
@@ -795,6 +795,40 @@ def wait_for_error_to_complete
end
end
+ context "exception app" do
+ before(:all) do
+ @app = lambda do |env|
+ if env['PATH_INFO'] == "/error"
+ raise "I am an error"
+ else
+ [200, { 'Content-Type' => 'text/html', 'Content-Length' => "0" }, [""]]
+ end
+ end
+ end
+
+ it "reraises the exception from the server thread" do
+ expect {
+ subject.visit("/error")
+ wait_for_error_to_complete
+ subject.find("//body")
+ }.to raise_error(StandardError, /I am an error/)
+ end
+
+ it "clears exceptions after resetting" do
+ expect {
+ subject.visit("/error")
+ wait_for_error_to_complete
+ subject.reset!
+ subject.visit("/")
+ subject.find("//body")
+ }.not_to raise_error
+ end
+
+ def wait_for_error_to_complete
+ sleep(0.5)
+ end
+ end
+
context "slow error app" do
before(:all) do
@app = lambda do |env|

0 comments on commit e3248b7

Please sign in to comment.