Fix overly specific exception catching in Runner.php#6209
Open
Fix overly specific exception catching in Runner.php#6209
Conversation
Changed catch block in Runner::run_command() from catching only WP_CLI\Iterators\Exception to catching all Throwable types. This ensures that exceptions thrown by commands are properly transformed to WP-CLI error messages instead of bubbling up to WordPress's error handler. Also added Behat test to verify the fix works correctly. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix overly specific exception handling in Runner.php
Fix overly specific exception catching in Runner.php
Jan 25, 2026
This comment was marked as resolved.
This comment was marked as resolved.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Instead of passing just the exception message, now passing the entire exception object. This causes WP_CLI::error_to_string() to prepend the exception class name to the error message, providing users with more context about what type of exception was thrown. Updated Behat tests to match the new expected output format. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Changed from catching \Throwable to catching \Exception to allow fatal errors (Error types) to be handled by ShutdownHandler. This ensures that plugin/theme errors get helpful skip suggestions instead of being caught and displayed as generic errors. \Throwable catches both Exception and Error, but Error types represent fatal errors that need special handling by ShutdownHandler for better user experience. Fixes failing test in shutdown-handler.feature. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix overly specific exception catching in Runner.php
Plan:
WP_CLI\Iterators\Exception)Issue Summary:
Currently,
Runner::run_command()only catchesWP_CLI\Iterators\Exception. Other exceptions bubble up to WordPress's error handler and display a generic fatal error instead of proper WP-CLI error output.Solution:
Changed the catch block to catch
\Exceptiontypes (not\Throwable) and pass the full exception object toWP_CLI::error()so they are properly transformed to WP-CLI error messages with exception class context. We specifically avoid catching\Errortypes so that fatal errors can be handled byShutdownHandlerfor helpful plugin/theme skip suggestions.Changes Made:
php/WP_CLI/Runner.phpto catch\Exception(not\Throwable) instead of justWP_CLI\Iterators\ExceptionWP_CLI::error()instead of just the messageWP_CLI\Iterators\Exceptionfeatures/runner.featureto verify exception handlingTests & Verification:
Error: Exception: Test exception messageError: RuntimeException: Test runtime exception messageTechnical Details:
\Throwableis the base interface for both\Exceptionand\Error(PHP 7+)\Exceptionrepresents recoverable exceptions that we want to catch\Errorrepresents fatal errors (e.g., calling undefined functions) that should be handled byShutdownHandlerfor better user experience with plugin/theme skip suggestionsExample Output:
Exceptions:
Error: Exception: Test exception messageFatal Errors: Handled by ShutdownHandler with plugin/theme suggestions
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.