Skip to content

fix: Browser#create_page ensure block masks original error with NoMethodError#582

Open
collin-pereira wants to merge 1 commit intorubycdp:mainfrom
collin-pereira:fix/create-page-nil-context-ensure
Open

fix: Browser#create_page ensure block masks original error with NoMethodError#582
collin-pereira wants to merge 1 commit intorubycdp:mainfrom
collin-pereira:fix/create-page-nil-context-ensure

Conversation

@collin-pereira
Copy link
Copy Markdown

@collin-pereira collin-pereira commented Apr 28, 2026

Problem

When Browser#create_page is called with new_context: true and a block, an exception raised by contexts.create(...) (for example Ferrum::DeadBrowserError when the browser dies before the context is created) is masked by a NoMethodError from the ensure block.

The local context is nil at that point because the assignment never completed. The ensure block then calls context.dispose unconditionally and raises NoMethodError: undefined method 'dispose' for nil, which propagates in place of the original error. Callers who rescue Ferrum::Error (the gem's documented contract) won't catch the masked NoMethodError, so legitimate errors like DeadBrowserError slip past their handlers.

Fix

The adjacent cleanup of page already uses safe navigation (page&.close) for the same reason; this just makes context.dispose symmetric.

A regression spec stubs browser.contexts.create to raise Ferrum::DeadBrowserError and asserts the original error propagates through the block form. Without the fix it fails with expected Ferrum::DeadBrowserError ... got NoMethodError: undefined method 'dispose' for nil.

@collin-pereira collin-pereira force-pushed the fix/create-page-nil-context-ensure branch from c5f4147 to 0ff8879 Compare April 28, 2026 11:51
@collin-pereira collin-pereira changed the title Fix Browser#create_page masking original error when context creation fails fix: Browser#create_page masking original error when context creation fails Apr 28, 2026
@collin-pereira collin-pereira changed the title fix: Browser#create_page masking original error when context creation fails fix: Browser#create_page ensure block masks original error with NoMethodError Apr 28, 2026
@collin-pereira collin-pereira force-pushed the fix/create-page-nil-context-ensure branch from 0ff8879 to f6e4ce7 Compare April 28, 2026 13:22
@collin-pereira collin-pereira marked this pull request as ready for review April 28, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant