Skip to content

Commit

Permalink
fix: Check if exchange exists before manipulating it (#442)
Browse files Browse the repository at this point in the history
* fix: Check if exchange exists before manipulating it

* chore: Add CHANGELOG entry
  • Loading branch information
route authored Feb 6, 2024
1 parent 0d1b3e9 commit 5b8b323
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ instead of passing browser and making cyclic dependency on the browser instance,
- Exceptions within `.on()` were swallowed by a thread pool of `Concurrent::Async` [#432]
- `Ferrum::Context#add_target` puts wrong target to pendings sometimes [#433]
- Leaking connection descriptors in tests and after browser quit [#433]
- Check if network exchange exists before manipulating it [#442]

### Removed

Expand Down
18 changes: 10 additions & 8 deletions lib/ferrum/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,19 +385,19 @@ def subscribe_request_will_be_sent
def subscribe_response_received
@page.on("Network.responseReceived") do |params|
exchange = select(params["requestId"]).last
next unless exchange

if exchange
response = Network::Response.new(@page, params)
exchange.response = response
end
response = Network::Response.new(@page, params)
exchange.response = response
end
end

def subscribe_loading_finished
@page.on("Network.loadingFinished") do |params|
response = select(params["requestId"]).last&.response
exchange = select(params["requestId"]).last
next unless exchange

if response
if (response = exchange.response)
response.loaded = true
response.body_size = params["encodedDataLength"]
end
Expand All @@ -407,8 +407,9 @@ def subscribe_loading_finished
def subscribe_loading_failed
@page.on("Network.loadingFailed") do |params|
exchange = select(params["requestId"]).last
exchange.error ||= Network::Error.new
next unless exchange

exchange.error ||= Network::Error.new
exchange.error.id = params["requestId"]
exchange.error.type = params["type"]
exchange.error.error_text = params["errorText"]
Expand All @@ -422,8 +423,9 @@ def subscribe_log_entry_added
entry = params["entry"] || {}
if entry["source"] == "network" && entry["level"] == "error"
exchange = select(entry["networkRequestId"]).last
exchange.error ||= Network::Error.new
next unless exchange

exchange.error ||= Network::Error.new
exchange.error.id = entry["networkRequestId"]
exchange.error.url = entry["url"]
exchange.error.description = entry["text"]
Expand Down

0 comments on commit 5b8b323

Please sign in to comment.