Skip to content

Commit 39c6d92

Browse files
committed
Improve error handling and add tests
1 parent 991e191 commit 39c6d92

File tree

2 files changed

+331
-35
lines changed

2 files changed

+331
-35
lines changed

src/wp-includes/template.php

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

966966
$filtered_output = $output;
967967

968-
$error_log = array();
968+
$error_log = array();
969+
set_error_handler(
970+
static function ( int $level, string $message, ?string $file = null, ?int $line = null ) use ( &$error_log ) {
971+
// Switch a user error to an exception so that it can be caught and the buffer can be returned.
972+
if ( E_USER_ERROR === $level ) {
973+
throw new Exception( __( 'User error triggered:' ) . ' ' . $message );
974+
}
975+
976+
if ( error_reporting() & $level ) {
977+
$error_log[] = compact( 'level', 'message', 'file', 'line' );
978+
}
979+
return false;
980+
}
981+
);
969982
$display_errors = ini_get( 'display_errors' );
970983
if ( $display_errors ) {
971984
ini_set( 'display_errors', 0 );
972-
set_error_handler(
973-
static function ( int $level, string $message, ?string $file = null, ?int $line = null, ?array $context = null ) use ( &$error_log ) {
974-
if ( error_reporting() & $level ) {
975-
$error_log[] = compact( 'level', 'message', 'file', 'line', 'context' );
976-
}
977-
return false;
978-
}
979-
);
980985
}
981986

982987
try {
@@ -1001,20 +1006,13 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10011006
*/
10021007
$filtered_output = (string) apply_filters( 'wp_template_enhancement_output_buffer', $filtered_output, $output );
10031008
} catch ( Exception $exception ) {
1004-
$error_log[] = array(
1005-
'level' => E_USER_ERROR,
1006-
'message' => $exception->getMessage(),
1007-
'file' => $exception->getFile(),
1008-
'line' => $exception->getLine(),
1009-
);
1010-
10111009
// Emit to the error log.
10121010
trigger_error(
10131011
sprintf(
1014-
/* translators: %s is wp_template_enhancement_output_buffer */
1015-
__( 'Exception thrown during %s filter: ' ) . $exception->getMessage(),
1016-
'wp_template_enhancement_output_buffer'
1017-
),
1012+
/* translators: %s is the exception class name */
1013+
__( 'Uncaught exception "%s" thrown:' ),
1014+
get_class( $exception )
1015+
) . ' ' . $exception->getMessage(),
10181016
E_USER_WARNING
10191017
);
10201018
}
@@ -1034,14 +1032,12 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10341032
default:
10351033
$type = 'Error';
10361034
}
1037-
$displayed_error = sprintf( "<br />\n<b>%s</b>: %s", $type, $error['message'] );
1038-
if ( null !== $error['file'] ) {
1039-
$displayed_error .= sprintf( ' in <b>%s</b>', $error['file'] );
1040-
if ( null !== $error['line'] ) {
1041-
$displayed_error .= sprintf( ' on line <b>%d</b>', $error['line'] );
1042-
}
1035+
$format = "<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%d</b><br />";
1036+
if ( ! ini_get( 'html_errors' ) ) {
1037+
$format = strip_tags( $format );
10431038
}
1044-
$displayed_error .= '<br />';
1039+
1040+
$displayed_error = sprintf( $format, $type, $error['message'], $error['file'], $error['line'] );
10451041

10461042
$filtered_output .= $displayed_error;
10471043
}
@@ -1086,9 +1082,9 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10861082
}
10871083

10881084
if ( $display_errors ) {
1089-
restore_error_handler();
10901085
ini_set( 'display_errors', 1 );
10911086
}
1087+
restore_error_handler();
10921088

10931089
return $filtered_output;
10941090
}

0 commit comments

Comments
 (0)