Make WordPress Core


Ignore:
Timestamp:
02/13/2024 09:08:43 AM (3 months ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r57553 r57610  
    40524052    );
    40534053
     4054    if ( isset( $parsed_args['error_data'] ) ) {
     4055        $data['data']['error'] = $parsed_args['error_data'];
     4056    }
     4057
    40544058    if ( ! headers_sent() ) {
    40554059        header( "Content-Type: application/json; charset={$parsed_args['charset']}" );
     
    40894093        'additional_errors' => $parsed_args['additional_errors'],
    40904094    );
     4095
     4096    if ( isset( $parsed_args['error_data'] ) ) {
     4097        $data['data']['error'] = $parsed_args['error_data'];
     4098    }
    40914099
    40924100    if ( ! headers_sent() ) {
     
    42664274            if ( empty( $title ) && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['title'] ) ) {
    42674275                $title = $errors[0]['data']['title'];
     4276            }
     4277            if ( WP_DEBUG_DISPLAY && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['error'] ) ) {
     4278                $args['error_data'] = $errors[0]['data']['error'];
    42684279            }
    42694280
Note: See TracChangeset for help on using the changeset viewer.