[wp-trac] [WordPress Trac] #60014: REST endpoint: Output "server errors" if WP_DEBUG = true (register_rest_route)

WordPress Trac noreply at wordpress.org
Thu Dec 7 10:58:27 UTC 2023


#60014: REST endpoint: Output "server errors" if WP_DEBUG = true
(register_rest_route)
-------------------------+------------------------------
 Reporter:  ecc          |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  REST API     |     Version:  6.4
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:  rest-api
-------------------------+------------------------------

Comment (by ecc):

 I updated the **_wp_die_process_input()** method (''functions.php, line
 4214'') in order to output the errors, if debug & display of errors is
 activated. This will output the exception/error in the JSON field
 **"additional_errors"**. (see below) This works for me.

 The changed lines are:

 Before

 {{{#!php
 unset( $errors[0] );
 }}}

 After:

 {{{#!php
 // Skip error reset, if debug & display of errors is activated
 if( (WP_DEBUG && ! WP_DEBUG_DISPLAY) || ! WP_DEBUG ) {
   unset( $errors[0] );
 }
 }}}

 Full updated code of _wp_die_process_input() :

 {{{#!php
 function _wp_die_process_input( $message, $title = '', $args = array() ) {
   $defaults = array(
     'response'          => 0,
     'code'              => '',
     'exit'              => true,
     'back_link'         => false,
     'link_url'          => '',
     'link_text'         => '',
     'text_direction'    => '',
     'charset'           => 'utf-8',
     'additional_errors' => array(),
   );

   $args = wp_parse_args( $args, $defaults );

   if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
     if ( ! empty( $message->errors ) ) {
       $errors = array();
       foreach ( (array) $message->errors as $error_code => $error_messages
 ) {
         foreach ( (array) $error_messages as $error_message ) {
           $errors[] = array(
             'code'    => $error_code,
             'message' => $error_message,
             'data'    => $message->get_error_data( $error_code ),
           );
         }
       }

       $message = $errors[0]['message'];
       if ( empty( $args['code'] ) ) {
         $args['code'] = $errors[0]['code'];
       }
       if ( empty( $args['response'] ) && is_array( $errors[0]['data'] ) &&
 ! empty( $errors[0]['data']['status'] ) ) {
         $args['response'] = $errors[0]['data']['status'];
       }
       if ( empty( $title ) && is_array( $errors[0]['data'] ) && ! empty(
 $errors[0]['data']['title'] ) ) {
         $title = $errors[0]['data']['title'];
       }

       // Skip error reset, if debug & display of errors is activated
       if( (WP_DEBUG && ! WP_DEBUG_DISPLAY) || ! WP_DEBUG ) {
         unset( $errors[0] );
       }

       $args['additional_errors'] = array_values( $errors );
     } else {
       $message = '';
     }
   }

   $have_gettext = function_exists( '__' );

   // The $title and these specific $args must always have a non-empty
 value.
   if ( empty( $args['code'] ) ) {
     $args['code'] = 'wp_die';
   }
   if ( empty( $args['response'] ) ) {
     $args['response'] = 500;
   }
   if ( empty( $title ) ) {
     $title = $have_gettext ? __( 'WordPress › Error' ) : 'WordPress
 › Error';
   }
   if ( empty( $args['text_direction'] ) || ! in_array(
 $args['text_direction'], array( 'ltr', 'rtl' ), true ) ) {
     $args['text_direction'] = 'ltr';
     if ( function_exists( 'is_rtl' ) && is_rtl() ) {
       $args['text_direction'] = 'rtl';
     }
   }

   if ( ! empty( $args['charset'] ) ) {
     $args['charset'] = _canonical_charset( $args['charset'] );
   }

   return array( $message, $title, $args );
 }
 }}}


 JSON response, if debug and debug_display is on:


 {{{
 {
     "code": "internal_server_error",
     "message": "<p>There has been a critical error on this
 website.<\/p><p><a href=\"https:\/\/wordpress.org\/documentation\/article
 \/faq-troubleshooting\/\">Learn more about troubleshooting
 WordPress.<\/a><\/p>",
     "data": {
         "status": 500
     },
     "additional_errors": [
         {
             "code": "internal_server_error",
             "message": "<p>There has been a critical error on this
 website.<\/p><p><a href=\"https:\/\/wordpress.org\/documentation\/article
 \/faq-troubleshooting\/\">Learn more about troubleshooting
 WordPress.<\/a><\/p>",
             "data": {
                 "error": {
                     "type": 1,
                     "message": "Uncaught InvalidArgumentException:
 Unexpected input for key ...",
                     "line": 120
                 }
             }
         }
     ]
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60014#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list