From 31a092799f06af16e71dac9f7b314f0d147152cd Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Tue, 13 Feb 2024 09:10:08 +0000 Subject: [PATCH] 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 --- wp-includes/functions.php | 11 +++++++++++ wp-includes/version.php | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 461918bd53..a3abfae9cf 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -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 ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 256f9dbb50..9581a74971 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.