Skip to content

Commit ef1b4b4

Browse files
committed
Display exceptions and user errors as errors not warnings
1 parent 39c6d92 commit ef1b4b4

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/wp-includes/template.php

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

966966
$filtered_output = $output;
967967

968+
$did_just_catch_exception = false;
969+
968970
$error_log = array();
969971
set_error_handler(
970-
static function ( int $level, string $message, ?string $file = null, ?int $line = null ) use ( &$error_log ) {
972+
static function ( int $level, string $message, ?string $file = null, ?int $line = null ) use ( &$error_log, &$did_just_catch_exception ) {
971973
// Switch a user error to an exception so that it can be caught and the buffer can be returned.
972974
if ( E_USER_ERROR === $level ) {
973975
throw new Exception( __( 'User error triggered:' ) . ' ' . $message );
974976
}
975977

978+
// Display a caught exception as an error since it prevents any of the output buffer filters from applying.
979+
if ( $did_just_catch_exception ) {
980+
$level = E_USER_ERROR;
981+
}
982+
976983
if ( error_reporting() & $level ) {
977984
$error_log[] = compact( 'level', 'message', 'file', 'line' );
978985
}
@@ -1006,7 +1013,9 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10061013
*/
10071014
$filtered_output = (string) apply_filters( 'wp_template_enhancement_output_buffer', $filtered_output, $output );
10081015
} catch ( Exception $exception ) {
1009-
// Emit to the error log.
1016+
$did_just_catch_exception = true;
1017+
1018+
// Emit to the error log as a warning not as an error to prevent halting execution.
10101019
trigger_error(
10111020
sprintf(
10121021
/* translators: %s is the exception class name */
@@ -1016,6 +1025,7 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10161025
E_USER_WARNING
10171026
);
10181027
}
1028+
$did_just_catch_exception = false;
10191029

10201030
if ( $display_errors ) {
10211031
foreach ( $error_log as $error ) {
@@ -1036,10 +1046,7 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10361046
if ( ! ini_get( 'html_errors' ) ) {
10371047
$format = strip_tags( $format );
10381048
}
1039-
1040-
$displayed_error = sprintf( $format, $type, $error['message'], $error['file'], $error['line'] );
1041-
1042-
$filtered_output .= $displayed_error;
1049+
$filtered_output .= sprintf( $format, $type, $error['message'], $error['file'], $error['line'] );
10431050
}
10441051
}
10451052

tests/phpunit/tests/template.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10451045
'PHP Warning: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified? in __FILE__ on line __LINE__',
10461046
),
10471047
'expected_displayed_errors' => array(
1048-
'<b>Warning</b>: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified? in <b>__FILE__</b> on line <b>__LINE__</b>',
1048+
'<b>Error</b>: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified? in <b>__FILE__</b> on line <b>__LINE__</b>',
10491049
),
10501050
),
10511051
'exception' => array(
@@ -1058,7 +1058,7 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10581058
'PHP Warning: Uncaught exception "Exception" thrown: I take exception to this! in __FILE__ on line __LINE__',
10591059
),
10601060
'expected_displayed_errors' => array(
1061-
'<b>Warning</b>: Uncaught exception "Exception" thrown: I take exception to this! in <b>__FILE__</b> on line <b>__LINE__</b>',
1061+
'<b>Error</b>: Uncaught exception "Exception" thrown: I take exception to this! in <b>__FILE__</b> on line <b>__LINE__</b>',
10621062
),
10631063
),
10641064
'multiple_non_errors' => array(

0 commit comments

Comments
 (0)