Skip to content

Commit 9160482

Browse files
Code Modernization: Fix trigger_error() with E_USER_ERROR deprecation in wp_trigger_error().
PHP 8.4 deprecates the use of `trigger_errror()` with `E_USER_ERROR` as the error level, as there are a number of gotchas to this way of creating a `Fatal Error` (`finally` blocks not executing, destructors not executing). The recommended replacements are either to use exceptions or to do a hard `exit`. WP has its own `wp_trigger_error()` function, which under the hood calls `trigger_error()`. If passed `E_USER_ERROR` as the `$error_level`, this will hit the PHP 8.4 deprecation. Now, there were basically three options: * Silence the deprecation until PHP 9.0 and delay properly solving this until then. This would lead to an awkward solution, as prior to PHP 8.0, error silencing would apply to all errors, while, as of PHP 8.0, it will no longer apply to fatal errors. It also would only buy us some time and wouldn't actually solve anything. * Use `exit($status)` when `wp_trigger_error()` is called with `E_USER_ERROR`. This would make the code untestable and would disable handling of these errors via custom error handlers, which makes this an undesirable solution. * Throw an exception when `wp_trigger_error()` is called with `E_USER_ERROR`. This makes for the most elegant solution with the least BC-breaking impact, though it does open it up to the error potential being "caught" via a `try-catch`. That's not actually a bad thing and is likely to only happen for those errors which can be worked around, in which case, it's a bonus that that's now possible. The third option is implemented which: * Introduces a new `WP_Exception` class. * Starts using `WP_Exception` in the `wp_trigger_error()` function when the `$error_level` is set to `E_USER_ERROR`. This change is covered by pre-existing tests, which have been updated to expect the exception instead of a PHP error. Why not use `WP_Error`? Well, for one, this would lead to completely different behaviour (BC). As `WP_Error` doesn't extend `Exception`, the program would not be stopped, but would continue running, which would be a much bigger breaking change and carries security risks. `WP_Error` also doesn't natively trigger displaying/logging of the error message, so in that case, it would still need an `exit` with the error message, bringing us back to point 2 above. Introducing `WP_Exception` provides (essentially) the same behaviour in that it retains the fatal error and error message displaying/logging behaviors. It also introduces a base Exception class, from which future exception classes can extend. References: * https://wiki.php.net/rfc/deprecations_php_8_4#deprecate_passing_e_user_error_to_trigger_error * https://www.php.net/manual/en/migration80.incompatible.php Follow-up to [56530]. Props jrf, hellofromTonya. See #62061. Built from https://develop.svn.wordpress.org/trunk@59107 git-svn-id: http://core.svn.wordpress.org/trunk@58503 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent e97af52 commit 9160482

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

wp-includes/class-wp-exception.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* WP_Exception class
4+
*
5+
* @package WordPress
6+
* @since 6.7.0
7+
*/
8+
9+
/**
10+
* Core base Exception class.
11+
*
12+
* Future, more specific, Exceptions should always extend this base class.
13+
*
14+
* @since 6.7.0
15+
*/
16+
class WP_Exception extends Exception {}

wp-includes/functions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6092,6 +6092,10 @@ function wp_trigger_error( $function_name, $message, $error_level = E_USER_NOTIC
60926092
array( 'http', 'https' )
60936093
);
60946094

6095+
if ( E_USER_ERROR === $error_level ) {
6096+
throw new WP_Exception( $message );
6097+
}
6098+
60956099
trigger_error( $message, $error_level );
60966100
}
60976101

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '6.7-alpha-59106';
19+
$wp_version = '6.7-alpha-59107';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

wp-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
// Include files required for initialization.
4141
require ABSPATH . WPINC . '/class-wp-paused-extensions-storage.php';
42+
require ABSPATH . WPINC . '/class-wp-exception.php';
4243
require ABSPATH . WPINC . '/class-wp-fatal-error-handler.php';
4344
require ABSPATH . WPINC . '/class-wp-recovery-mode-cookie-service.php';
4445
require ABSPATH . WPINC . '/class-wp-recovery-mode-key-service.php';

0 commit comments

Comments
 (0)