[wp-trac] [WordPress Trac] #46191: WP_REST_Request::has_valid_params() should utilize get_error_messages() and not get_error_message(). Unexpected behavior occurs when you try to WP_Error->add() in the REST API

WordPress Trac noreply at wordpress.org
Mon Feb 1 22:14:39 UTC 2021


#46191: WP_REST_Request::has_valid_params() should utilize get_error_messages() and
not get_error_message(). Unexpected behavior occurs when you try to
WP_Error->add() in the REST API
--------------------------------------+-----------------------
 Reporter:  tmfespresso               |       Owner:  (none)
     Type:  defect (bug)              |      Status:  new
 Priority:  normal                    |   Milestone:  5.7
Component:  REST API                  |     Version:  4.4
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:  rest-api
--------------------------------------+-----------------------

Comment (by xkon):

 PR looks good to me! A couple of minor things noticed, not blockers in any
 way but we could consider them for making the response a bit easier to
 read & follow:

 One is the error statuses that are returned on all instances ( see
 following example code & responses ) we always have x2 `status` as an
 example with this test code. Ideally we should be able to show an 1:1
 relationship of which status goes with which error as it might be a bit
 confusing at the moment. This might need to be dealt within WP_Error
 though.

 Also maybe we can clean up the `additional_errors` a bit by not
 duplicating the `"data"` as well on each iteration.

 ---

 For testing purposes I'm adding a simple snippet ( can be used as a mu-
 plugin ).

 Can be tested with Postman via an ( example ) :

 POST https://core.local/src/index.php?rest_route=/ticket/v1/test&id=1

 {{{
 <?php

 add_action( 'rest_api_init', 'add_custom_users_api');
 function add_custom_users_api() {
         register_rest_route(
                 'ticket/v1',
                 '/test',
                 array(
                         'methods' => 'POST',
                         'callback' => 'my_ticket_test',
                         'args' => array(
                                 'id' => array(
                                         'validate_callback' =>
 'test_validate'
                                 ),
                         ),
                 )
         );
 }

 function test_validate( $id ) {

         $error = new \WP_Error( 'rest_test_error', 'This is our first
 error.', array( 'status' => 410 ) );

         if ( is_numeric( $id ) ) {
                 $error->add( 'rest_test_error', 'Well this is yet another
 error :D.', array( 'status' => 420 ) );

                 return $error;
         }

         return true;
 }

 function my_ticket_test( \WP_REST_Request $request ) {
         return 'success';
 }
 }}}

 ---

 Pre-patch the response would've been:

 {{{
 {
     "code": "rest_invalid_param",
     "message": "Invalid parameter(s): id",
     "data": {
         "status": 400,
         "params": {
             "id": "This is our first error."
         }
     }
 }
 }}}

 After applying PR we get:

 {{{
 {
     "code": "rest_invalid_param",
     "message": "Invalid parameter(s): id",
     "data": {
         "status": 400,
         "params": {
             "id": "This is our first error. Well this is yet another error
 :D."
         }
     },
     "additional_errors": [
         {
             "code": "rest_test_error",
             "message": "This is our first error.",
             "data": {
                 "param": "id"
             },
             "param": "id",
             "additional_data": [
                 {
                     "status": 420
                 },
                 {
                     "status": 410
                 }
             ]
         },
         {
             "code": "rest_test_error",
             "message": "Well this is yet another error :D.",
             "data": {
                 "param": "id"
             },
             "param": "id",
             "additional_data": [
                 {
                     "status": 420
                 },
                 {
                     "status": 410
                 }
             ]
         }
     ]
 }
 }}}

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


More information about the wp-trac mailing list