REST API: Provide detailed error data in REST API response.

When the fatal error handler is triggered within a REST API request, it currently utilizes wp_die to display a specially formatted error response. However, crucial information captured by the fatal error handler, such as the exact line where the error occurred, is not included in the response due to potential security concerns, such as leaking file paths.

To address this limitation and aid developers in debugging, this enhancement introduces the inclusion of error data in the response when the `WP_DEBUG_DISPLAY` constant is set to true. This additional data, appended under the new key error_data, will facilitate more thorough debugging for REST API errors.

Props ecc, spacedmonkey, TimothyBlynJacobs, rcorrales.
Fixes #60014.
Built from https://develop.svn.wordpress.org/trunk@57610


git-svn-id: http://core.svn.wordpress.org/trunk@57111 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2024-02-13 09:10:08 +00:00
parent d6728e990e
commit 31a092799f
2 changed files with 12 additions and 1 deletions

View File

@ -4051,6 +4051,10 @@ function _json_wp_die_handler( $message, $title = '', $args = array() ) {
'additional_errors' => $parsed_args['additional_errors'],
);
if ( isset( $parsed_args['error_data'] ) ) {
$data['data']['error'] = $parsed_args['error_data'];
}
if ( ! headers_sent() ) {
header( "Content-Type: application/json; charset={$parsed_args['charset']}" );
if ( null !== $parsed_args['response'] ) {
@ -4089,6 +4093,10 @@ function _jsonp_wp_die_handler( $message, $title = '', $args = array() ) {
'additional_errors' => $parsed_args['additional_errors'],
);
if ( isset( $parsed_args['error_data'] ) ) {
$data['data']['error'] = $parsed_args['error_data'];
}
if ( ! headers_sent() ) {
header( "Content-Type: application/javascript; charset={$parsed_args['charset']}" );
header( 'X-Content-Type-Options: nosniff' );
@ -4266,6 +4274,9 @@ function _wp_die_process_input( $message, $title = '', $args = array() ) {
if ( empty( $title ) && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['title'] ) ) {
$title = $errors[0]['data']['title'];
}
if ( WP_DEBUG_DISPLAY && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['error'] ) ) {
$args['error_data'] = $errors[0]['data']['error'];
}
unset( $errors[0] );
$args['additional_errors'] = array_values( $errors );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-alpha-57609';
$wp_version = '6.5-alpha-57610';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.