Skip to content

Commit a6708f9

Browse files
Raise StatusError on top frame navigation error (#341)
1 parent 0cab51d commit a6708f9

File tree

6 files changed

+12
-38
lines changed

6 files changed

+12
-38
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,9 @@ Activates offline mode for a page.
595595

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

601-
602601
## Proxy
603602

604603
You can set a proxy with a `:proxy` option:

lib/ferrum/network.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,7 @@ def emulate_network_conditions(offline: false, latency: 0,
333333
#
334334
# @example
335335
# browser.network.offline_mode
336-
# browser.go_to("https://github.com/") # => Ferrum::StatusError (Request to https://github.com/ failed to reach
337-
# server, check DNS and server status)
336+
# browser.go_to("https://github.com/") # => Ferrum::StatusError (Request to https://github.com/ failed (net::ERR_INTERNET_DISCONNECTED))
338337
#
339338
def offline_mode
340339
emulate_network_conditions(offline: true, latency: 0, download_throughput: 0, upload_throughput: 0)

lib/ferrum/page.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,7 @@ def go_to(url = nil)
114114
options = { url: combine_url!(url) }
115115
options.merge!(referrer: referrer) if referrer
116116
response = command("Page.navigate", wait: GOTO_WAIT, **options)
117-
# https://cs.chromium.org/chromium/src/net/base/net_error_list.h
118-
if %w[net::ERR_NAME_NOT_RESOLVED
119-
net::ERR_NAME_RESOLUTION_FAILED
120-
net::ERR_INTERNET_DISCONNECTED
121-
net::ERR_CONNECTION_TIMED_OUT
122-
net::ERR_FILE_NOT_FOUND].include?(response["errorText"])
123-
raise StatusError, options[:url]
124-
end
117+
raise StatusError.new(options[:url], "Request to #{options[:url]} failed (#{response['errorText']})") if response['errorText']
125118

126119
response["frameId"]
127120
rescue TimeoutError

spec/network/exchange_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@
9191
network.intercept
9292
page.on(:request) { |r, _, _| r.abort }
9393

94-
page.go_to
94+
expect do
95+
page.go_to
96+
end.to raise_error(
97+
Ferrum::StatusError,
98+
%r{Request to http://127.0.0.1:#{server.port} failed \(net::ERR_BLOCKED_BY_CLIENT\)}
99+
)
95100

96101
expect(page.body).not_to include("Hello world!")
97102
expect(last_exchange.blocked?).to be true

spec/network_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@
456456

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

462462
expect(page.body).to eq("<html><head></head><body></body></html>")

spec/page_spec.rb

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,10 @@
1616
end
1717

1818
context "with failing response" do
19-
it "handles when a non-existent file was specified" do
20-
file_name = "file:non-existent"
21-
22-
expect do
23-
page.go_to(file_name)
24-
end.to raise_error(
25-
Ferrum::StatusError,
26-
"Request to #{file_name} failed to reach server, check DNS and server status"
27-
)
28-
end
29-
30-
it "handles when DNS is incorrect" do
19+
it "handles navigation error" do
3120
expect { page.go_to("http://nope:#{port}/") }.to raise_error(
3221
Ferrum::StatusError,
33-
%r{Request to http://nope:\d+/ failed to reach server, check DNS and server status}
34-
)
35-
end
36-
37-
it "has a descriptive message when DNS incorrect" do
38-
url = "http://nope:#{port}/"
39-
40-
expect do
41-
page.go_to(url)
42-
end.to raise_error(
43-
Ferrum::StatusError,
44-
/Request to #{url} failed to reach server, check DNS and server status/
22+
%r{Request to http://nope:\d+/ failed \(net::ERR_NAME_NOT_RESOLVED\)}
4523
)
4624
end
4725

0 commit comments

Comments
 (0)