WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/02/2021 05:26:06 PM (3 months ago)
Author:
TimothyBlynJacobs
Message:

REST API: Return detailed error information from request validation.

Previously, only the first error message for each parameter was made available. Now, all error messages for a parameter are concatenated. Additionally, the detailed error for each parameter is made available in a new details section of the validation error. Each error is formatted following the standard REST API error formatting.

The WP_REST_Server::error_to_response method has been abstracted out into a standalone function rest_convert_error_to_response to allow for reuse by WP_REST_Request. The formatted errors now also contain an additional_data property which contains the additional error data provided by WP_Error::get_all_error_data.

Props dlh, xkon, TimothyBlynJacobs.
Fixes #46191.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r50124 r50150  
    456456        $request->set_param( 'context', 'edit' );
    457457        $response = rest_get_server()->dispatch( $request );
    458         $data     = $response->get_data();
    459         $this->assertCount( 3, $data );
    460         $this->assertSame( 'rest_invalid_param', $data['code'] );
     458        $this->assertErrorResponse( 'rest_invalid_param', $response );
    461459    }
    462460
Note: See TracChangeset for help on using the changeset viewer.