Skip to content

fix: clear server exception on successful request and consumption - #324

Open
helmipradita wants to merge 2 commits into
pestphp:5.xfrom
helmipradita:fix/clear-stale-server-throwable-across-requests
Open

helmipradita wants to merge 2 commits into
pestphp:5.xfrom
helmipradita:fix/clear-stale-server-throwable-across-requests

Conversation

@helmipradita

Copy link
Copy Markdown
Contributor

Summary

When testing multi-step flows where an earlier request triggers an exception (e.g. asserting a 500 error or handling it via expect(...)->toThrow(...)), LaravelHttpServer::$lastThrowable previously remained populated. When subsequent requests succeeded without exceptions, $lastThrowable was not reset, causing throwLastThrowableIfNeeded() to erroneously re-throw the stale exception on later assertions. Furthermore, throwing in throwLastThrowableIfNeeded() did not clear the property, causing repeated throws.

Changes

  1. In LaravelHttpServer::throwLastThrowableIfNeeded(), clear $this->lastThrowable = null upon throwing so consumed exceptions are not re-thrown by subsequent checks.
  2. In request handling, explicitly set $this->lastThrowable = null when $response->exception is null.
  3. Added unit test in LaravelHttpServerTest.php verifying that a server exception on an earlier request does not leak into subsequent successful requests within the same test.

Fixes pestphp/pest#2009 (Case 2).

@helmipradita
helmipradita force-pushed the fix/clear-stale-server-throwable-across-requests branch from 7f49b94 to 5d8ec71 Compare November 20, 2026 12:57
@helmipradita

Copy link
Copy Markdown
Contributor Author

CI on this fork PR is action_required with 0 jobs — first on 7f49b94 (run 44233959508) and again on the re-pushed identical commit 5d8ec71 (run 44740056290). GitHub is holding the Tests workflow pending maintainer approval; no test has failed. @MrPunyapal could you approve the workflow so CI can run? The re-push only changed the commit SHA to retrigger — content is unchanged.

@MrPunyapal

Copy link
Copy Markdown
Member

Please clear the previous exception when a new navigation starts. A successful subrequest can currently erase the exception from the page that just failed. Add a regression test with a 500 page that loads a stylesheet, and chain an assertion onto visit('/failing-route') so the existing test actually makes the request.

LastThrowable was cleared whenever any request succeeded, so a stylesheet/script/XHR subrequest could erase the exception from the 500 page that just failed. Clear only on document/iframe navigation (or HTML accept fallback) at handleRequest start. Chain an assertion onto visit('/failing-route') so the existing regression test actually issues the request, and add a 500-page-loads-stylesheet case. Addresses MrPunyapal review on pestphp#324.

Signed-off-by: Helmi Pradita <helmipraditaa@gmail.com>
@helmipradita

Copy link
Copy Markdown
Contributor Author

Thanks @MrPunyapal — both points addressed in 76b42f6.

  • LaravelHttpServer no longer clears lastThrowable when a request succeeds. Clearing now happens at the start of a document/iframe navigation (sec-fetch-dest, with an HTML Accept fallback), so a successful stylesheet/script/XHR subrequest can't erase the exception from the 500 page it belongs to.
  • The existing regression test now chains assertPathIs('/failing-route') onto visit('/failing-route') so the request actually happens.
  • Added keeps the server exception when a 500 page loads a successful stylesheet: a 500 response carrying <link rel="stylesheet" href="/site.css"> and withException(...); /site.css returns 200. The original RuntimeException must still surface.

Checks run locally: php -l on both changed files, pint --test (2 files pass), phpstan analyse on LaravelHttpServer.php (no errors). Browser tests could not be executed in my sandbox — the Playwright driver here fails the pre-existing tests too with Target page, context or browser has been closed — so CI on the new head is the remaining verification. The action_required fork-PR workflow gate still needs a maintainer to approve the run.

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.

[Bug]: Browser plugin: a test that fails on its first check is reported as risky, and an earlier page's server exception reaches later pages

2 participants