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
This commit is contained in:
Jonny Harris 2024-02-13 09:08:43 +00:00
parent 079dd85e8b
commit 2a6fee5537

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 );