From 2a6fee5537402b6472ee5cc31a4d0f1f5d91e889 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 13 Feb 2024 09:08:43 +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. git-svn-id: https://develop.svn.wordpress.org/trunk@57610 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 461918bd53..a3abfae9cf 100644 --- a/src/wp-includes/functions.php +++ b/src/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 );