From 5b8b323cd4bf21ab6a6666959c2e423913e8babe Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Tue, 6 Feb 2024 12:09:57 +0500 Subject: [PATCH] fix: Check if exchange exists before manipulating it (#442) * fix: Check if exchange exists before manipulating it * chore: Add CHANGELOG entry --- CHANGELOG.md | 1 + lib/ferrum/network.rb | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b332b564..6ebbf6d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/ferrum/network.rb b/lib/ferrum/network.rb index 0301e61d..46845a40 100644 --- a/lib/ferrum/network.rb +++ b/lib/ferrum/network.rb @@ -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 @@ -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"] @@ -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"]