Skip to content

Commit 2d9bdd4

Browse files
westonruterdmsnell
andcommitted
Use Throwable instead of Exception when catching
Co-authored-by: Dennis Snell <[email protected]>
1 parent 2ef5af1 commit 2d9bdd4

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

src/wp-includes/template.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -965,18 +965,18 @@ function wp_finalize_template_enhancement_output_buffer( string $output, int $ph
965965

966966
$filtered_output = $output;
967967

968-
$did_just_catch_exception = false;
968+
$did_just_catch = false;
969969

970970
$error_log = array();
971971
set_error_handler(
972-
static function ( int $level, string $message, ?string $file = null, ?int $line = null ) use ( &$error_log, &$did_just_catch_exception ) {
972+
static function ( int $level, string $message, ?string $file = null, ?int $line = null ) use ( &$error_log, &$did_just_catch ) {
973973
// Switch a user error to an exception so that it can be caught and the buffer can be returned.
974974
if ( E_USER_ERROR === $level ) {
975975
throw new Exception( __( 'User error triggered:' ) . ' ' . $message );
976976
}
977977

978978
// Display a caught exception as an error since it prevents any of the output buffer filters from applying.
979-
if ( $did_just_catch_exception ) { // @phpstan-ignore if.alwaysFalse (The variable is set in the catch block below.)
979+
if ( $did_just_catch ) { // @phpstan-ignore if.alwaysFalse (The variable is set in the catch block below.)
980980
$level = E_USER_ERROR;
981981
}
982982

@@ -1017,18 +1017,18 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10171017
* @param string $output Original HTML template output buffer.
10181018
*/
10191019
$filtered_output = (string) apply_filters( 'wp_template_enhancement_output_buffer', $filtered_output, $output );
1020-
} catch ( Exception $exception ) {
1020+
} catch ( Throwable $throwable ) {
10211021
// Emit to the error log as a warning not as an error to prevent halting execution.
1022-
$did_just_catch_exception = true;
1022+
$did_just_catch = true;
10231023
trigger_error(
10241024
sprintf(
1025-
/* translators: %s is the exception class name */
1026-
__( 'Uncaught exception "%s" thrown:' ),
1027-
get_class( $exception )
1028-
) . ' ' . $exception->getMessage(),
1025+
/* translators: %s is the throwable class name */
1026+
__( 'Uncaught "%s" thrown:' ),
1027+
get_class( $throwable )
1028+
) . ' ' . $throwable->getMessage(),
10291029
E_USER_WARNING
10301030
);
1031-
$did_just_catch_exception = false;
1031+
$did_just_catch = false;
10321032
}
10331033

10341034
try {
@@ -1056,18 +1056,18 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10561056
* @param string $output Finalized output buffer.
10571057
*/
10581058
do_action( 'wp_finalized_template_enhancement_output_buffer', $filtered_output );
1059-
} catch ( Exception $exception ) {
1059+
} catch ( Throwable $throwable ) {
10601060
// Emit to the error log as a warning not as an error to prevent halting execution.
1061-
$did_just_catch_exception = true;
1061+
$did_just_catch = true;
10621062
trigger_error(
10631063
sprintf(
1064-
/* translators: %s is the exception class name */
1065-
__( 'Uncaught exception "%s" thrown:' ),
1066-
get_class( $exception )
1067-
) . ' ' . $exception->getMessage(),
1064+
/* translators: %s is the class name */
1065+
__( 'Uncaught "%s" thrown:' ),
1066+
get_class( $throwable )
1067+
) . ' ' . $throwable->getMessage(),
10681068
E_USER_WARNING
10691069
);
1070-
$did_just_catch_exception = false;
1070+
$did_just_catch = false;
10711071
}
10721072

10731073
// Append any errors to be displayed before returning flushing the buffer.

tests/phpunit/tests/template.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,12 +1060,12 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10601060
},
10611061
'expected_processed' => false,
10621062
'expected_error_log' => array(
1063-
'PHP Warning: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during filter? in __FILE__ on line __LINE__',
1064-
'PHP Warning: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during action? in __FILE__ on line __LINE__',
1063+
'PHP Warning: Uncaught "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during filter? in __FILE__ on line __LINE__',
1064+
'PHP Warning: Uncaught "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during action? in __FILE__ on line __LINE__',
10651065
),
10661066
'expected_displayed_errors' => array(
1067-
'<b>Error</b>: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during filter? in <b>__FILE__</b> on line <b>__LINE__</b>',
1068-
'<b>Error</b>: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during action? in <b>__FILE__</b> on line <b>__LINE__</b>',
1067+
'<b>Error</b>: Uncaught "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during filter? in <b>__FILE__</b> on line <b>__LINE__</b>',
1068+
'<b>Error</b>: Uncaught "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during action? in <b>__FILE__</b> on line <b>__LINE__</b>',
10691069
),
10701070
),
10711071
'exception' => array(
@@ -1078,12 +1078,12 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10781078
},
10791079
'expected_processed' => false,
10801080
'expected_error_log' => array(
1081-
'PHP Warning: Uncaught exception "Exception" thrown: I take exception to this filter! in __FILE__ on line __LINE__',
1082-
'PHP Warning: Uncaught exception "Exception" thrown: I take exception to this action! in __FILE__ on line __LINE__',
1081+
'PHP Warning: Uncaught "Exception" thrown: I take exception to this filter! in __FILE__ on line __LINE__',
1082+
'PHP Warning: Uncaught "Exception" thrown: I take exception to this action! in __FILE__ on line __LINE__',
10831083
),
10841084
'expected_displayed_errors' => array(
1085-
'<b>Error</b>: Uncaught exception "Exception" thrown: I take exception to this filter! in <b>__FILE__</b> on line <b>__LINE__</b>',
1086-
'<b>Error</b>: Uncaught exception "Exception" thrown: I take exception to this action! in <b>__FILE__</b> on line <b>__LINE__</b>',
1085+
'<b>Error</b>: Uncaught "Exception" thrown: I take exception to this filter! in <b>__FILE__</b> on line <b>__LINE__</b>',
1086+
'<b>Error</b>: Uncaught "Exception" thrown: I take exception to this action! in <b>__FILE__</b> on line <b>__LINE__</b>',
10871087
),
10881088
),
10891089
'multiple_non_errors' => array(

0 commit comments

Comments
 (0)