Skip to content

Commit de24fc3

Browse files
Copilotswissspidy
andcommitted
Simplify error handling to work with WP-CLI's built-in shutdown handler
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent a00b72d commit de24fc3

2 files changed

Lines changed: 2 additions & 57 deletions

File tree

features/core-install.feature

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,10 @@ Feature: Install WordPress core
381381
When I try `wp core install --url=example.org --title=Test --admin_user=testadmin --admin_email=testadmin@example.com --admin_password=testpass`
382382
Then STDERR should contain:
383383
"""
384-
Error: Failed to load WordPress files for WordPress installation
384+
Fatal error: Call to undefined function mysqli_connect()
385385
"""
386386
And STDERR should contain:
387387
"""
388-
This error is in WordPress core files, not a plugin or theme
389-
"""
390-
And STDERR should contain:
391-
"""
392-
It often indicates a missing PHP extension (like mysqli)
393-
"""
394-
And STDERR should contain:
395-
"""
396-
Call to undefined function mysqli_connect()
388+
Error: There has been a critical error on this website
397389
"""
398390
And the return code should be 255

src/Core_Command.php

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,55 +1968,8 @@ private function require_upgrade_file( $context = 'WordPress operation' ) {
19681968
WP_CLI::error( "Cannot read WordPress installation file '{$upgrade_file}'. Check file permissions." );
19691969
}
19701970

1971-
// Use a flag to track successful completion and prevent handler from executing after success.
1972-
$require_completed = false;
1973-
1974-
// Register a shutdown function to catch fatal errors during require_once.
1975-
$shutdown_handler = function () use ( $context, &$require_completed ) {
1976-
// Only handle errors if require_once did not complete successfully.
1977-
// @phpstan-ignore-next-line
1978-
if ( $require_completed ) {
1979-
return;
1980-
}
1981-
1982-
$error = error_get_last();
1983-
if (
1984-
null !== $error
1985-
&& in_array(
1986-
$error['type'],
1987-
[ E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR ],
1988-
true
1989-
)
1990-
) {
1991-
// Check if error occurred in the upgrade file or files it includes.
1992-
if (
1993-
false !== strpos( $error['file'], 'wp-admin/includes/' )
1994-
|| false !== strpos( $error['file'], 'wp-includes/' )
1995-
) {
1996-
// Provide a more specific error message for WordPress core file errors
1997-
// before WP-CLI's generic shutdown handler runs
1998-
$message = sprintf(
1999-
"Failed to load WordPress files for %s.\n\nError: %s in %s on line %d\n\nThis error is in WordPress core files, not a plugin or theme.\nIt often indicates a missing PHP extension (like mysqli) or corrupted WordPress installation.\n\nPlease check that all required PHP extensions are installed and that your WordPress installation is complete.",
2000-
$context,
2001-
$error['message'],
2002-
$error['file'],
2003-
$error['line']
2004-
);
2005-
2006-
// Output directly and exit to avoid WP-CLI's generic plugin/theme suggestion
2007-
fwrite( STDERR, "Error: {$message}\n" );
2008-
exit( 1 );
2009-
}
2010-
}
2011-
};
2012-
2013-
register_shutdown_function( $shutdown_handler );
2014-
20151971
// phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable -- Path comes from WordPress itself.
20161972
require_once $upgrade_file;
2017-
2018-
// Mark as completed to prevent the shutdown handler from executing on unrelated errors.
2019-
$require_completed = true;
20201973
}
20211974

20221975
/**

0 commit comments

Comments
 (0)