Skip to content

Commit

Permalink
fix: Check if exchange exists before manipulating it
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Feb 6, 2024
1 parent 0d1b3e9 commit 0cca033
Showing 1 changed file with 10 additions and 8 deletions.
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 0cca033

Please sign in to comment.