Skip to content

Commit 5b8b323

Browse files
authored
fix: Check if exchange exists before manipulating it (#442)
* fix: Check if exchange exists before manipulating it * chore: Add CHANGELOG entry
1 parent 0d1b3e9 commit 5b8b323

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ instead of passing browser and making cyclic dependency on the browser instance,
3030
- Exceptions within `.on()` were swallowed by a thread pool of `Concurrent::Async` [#432]
3131
- `Ferrum::Context#add_target` puts wrong target to pendings sometimes [#433]
3232
- Leaking connection descriptors in tests and after browser quit [#433]
33+
- Check if network exchange exists before manipulating it [#442]
3334

3435
### Removed
3536

lib/ferrum/network.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -385,19 +385,19 @@ def subscribe_request_will_be_sent
385385
def subscribe_response_received
386386
@page.on("Network.responseReceived") do |params|
387387
exchange = select(params["requestId"]).last
388+
next unless exchange
388389

389-
if exchange
390-
response = Network::Response.new(@page, params)
391-
exchange.response = response
392-
end
390+
response = Network::Response.new(@page, params)
391+
exchange.response = response
393392
end
394393
end
395394

396395
def subscribe_loading_finished
397396
@page.on("Network.loadingFinished") do |params|
398-
response = select(params["requestId"]).last&.response
397+
exchange = select(params["requestId"]).last
398+
next unless exchange
399399

400-
if response
400+
if (response = exchange.response)
401401
response.loaded = true
402402
response.body_size = params["encodedDataLength"]
403403
end
@@ -407,8 +407,9 @@ def subscribe_loading_finished
407407
def subscribe_loading_failed
408408
@page.on("Network.loadingFailed") do |params|
409409
exchange = select(params["requestId"]).last
410-
exchange.error ||= Network::Error.new
410+
next unless exchange
411411

412+
exchange.error ||= Network::Error.new
412413
exchange.error.id = params["requestId"]
413414
exchange.error.type = params["type"]
414415
exchange.error.error_text = params["errorText"]
@@ -422,8 +423,9 @@ def subscribe_log_entry_added
422423
entry = params["entry"] || {}
423424
if entry["source"] == "network" && entry["level"] == "error"
424425
exchange = select(entry["networkRequestId"]).last
425-
exchange.error ||= Network::Error.new
426+
next unless exchange
426427

428+
exchange.error ||= Network::Error.new
427429
exchange.error.id = entry["networkRequestId"]
428430
exchange.error.url = entry["url"]
429431
exchange.error.description = entry["text"]

0 commit comments

Comments
 (0)