Skip to content

Commit

Permalink
Raise StatusError on top frame navigation error (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
francisbeaudoin authored Feb 16, 2023
1 parent 0cab51d commit a6708f9
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 38 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,9 @@ Activates offline mode for a page.

```ruby
browser.network.offline_mode
browser.go_to("https://github.com/") # => Ferrum::StatusError (Request to https://github.com/ failed to reach server, check DNS and server status)
browser.go_to("https://github.com/") # => Ferrum::StatusError (Request to https://github.com/ failed(net::ERR_INTERNET_DISCONNECTED))
```


## Proxy

You can set a proxy with a `:proxy` option:
Expand Down
3 changes: 1 addition & 2 deletions lib/ferrum/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ def emulate_network_conditions(offline: false, latency: 0,
#
# @example
# browser.network.offline_mode
# browser.go_to("https://github.com/") # => Ferrum::StatusError (Request to https://github.com/ failed to reach
# server, check DNS and server status)
# browser.go_to("https://github.com/") # => Ferrum::StatusError (Request to https://github.com/ failed (net::ERR_INTERNET_DISCONNECTED))
#
def offline_mode
emulate_network_conditions(offline: true, latency: 0, download_throughput: 0, upload_throughput: 0)
Expand Down
9 changes: 1 addition & 8 deletions lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,7 @@ def go_to(url = nil)
options = { url: combine_url!(url) }
options.merge!(referrer: referrer) if referrer
response = command("Page.navigate", wait: GOTO_WAIT, **options)
# https://cs.chromium.org/chromium/src/net/base/net_error_list.h
if %w[net::ERR_NAME_NOT_RESOLVED
net::ERR_NAME_RESOLUTION_FAILED
net::ERR_INTERNET_DISCONNECTED
net::ERR_CONNECTION_TIMED_OUT
net::ERR_FILE_NOT_FOUND].include?(response["errorText"])
raise StatusError, options[:url]
end
raise StatusError.new(options[:url], "Request to #{options[:url]} failed (#{response['errorText']})") if response['errorText']

response["frameId"]
rescue TimeoutError
Expand Down
7 changes: 6 additions & 1 deletion spec/network/exchange_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@
network.intercept
page.on(:request) { |r, _, _| r.abort }

page.go_to
expect do
page.go_to
end.to raise_error(
Ferrum::StatusError,
%r{Request to http://127.0.0.1:#{server.port} failed \(net::ERR_BLOCKED_BY_CLIENT\)}
)

expect(page.body).not_to include("Hello world!")
expect(last_exchange.blocked?).to be true
Expand Down
2 changes: 1 addition & 1 deletion spec/network_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@

expect { page.go_to("/ferrum/with_js") }.to raise_error(
Ferrum::StatusError,
%r{Request to http://.*/ferrum/with_js failed to reach server, check DNS and server status}
%r{Request to http://.*/ferrum/with_js failed \(net::ERR_INTERNET_DISCONNECTED\)}
)

expect(page.body).to eq("<html><head></head><body></body></html>")
Expand Down
26 changes: 2 additions & 24 deletions spec/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,10 @@
end

context "with failing response" do
it "handles when a non-existent file was specified" do
file_name = "file:non-existent"

expect do
page.go_to(file_name)
end.to raise_error(
Ferrum::StatusError,
"Request to #{file_name} failed to reach server, check DNS and server status"
)
end

it "handles when DNS is incorrect" do
it "handles navigation error" do
expect { page.go_to("http://nope:#{port}/") }.to raise_error(
Ferrum::StatusError,
%r{Request to http://nope:\d+/ failed to reach server, check DNS and server status}
)
end

it "has a descriptive message when DNS incorrect" do
url = "http://nope:#{port}/"

expect do
page.go_to(url)
end.to raise_error(
Ferrum::StatusError,
/Request to #{url} failed to reach server, check DNS and server status/
%r{Request to http://nope:\d+/ failed \(net::ERR_NAME_NOT_RESOLVED\)}
)
end

Expand Down

0 comments on commit a6708f9

Please sign in to comment.