Make WordPress Core


Ignore:
Timestamp:
02/02/2021 05:26:06 PM (4 years 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/src/wp-includes/rest-api/class-wp-rest-request.php

    r49955 r50150  
    803803        $order = $this->get_parameter_order();
    804804
    805         $invalid_params = array();
     805        $invalid_params  = array();
     806        $invalid_details = array();
    806807
    807808        foreach ( $order as $type ) {
     
    826827                }
    827828
     829                /** @var mixed|WP_Error $sanitized_value */
    828830                $sanitized_value = call_user_func( $param_args['sanitize_callback'], $value, $this, $key );
    829831
    830832                if ( is_wp_error( $sanitized_value ) ) {
    831                     $invalid_params[ $key ] = $sanitized_value->get_error_message();
     833                    $invalid_params[ $key ]  = implode( ' ', $sanitized_value->get_error_messages() );
     834                    $invalid_details[ $key ] = rest_convert_error_to_response( $sanitized_value )->get_data();
    832835                } else {
    833836                    $this->params[ $type ][ $key ] = $sanitized_value;
     
    842845                sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ),
    843846                array(
    844                     'status' => 400,
    845                     'params' => $invalid_params,
     847                    'status'  => 400,
     848                    'params'  => $invalid_params,
     849                    'details' => $invalid_details,
    846850                )
    847851            );
     
    895899         * This is done after required checking as required checking is cheaper.
    896900         */
    897         $invalid_params = array();
     901        $invalid_params  = array();
     902        $invalid_details = array();
    898903
    899904        foreach ( $args as $key => $arg ) {
     
    902907
    903908            if ( null !== $param && ! empty( $arg['validate_callback'] ) ) {
     909                /** @var bool|\WP_Error $valid_check */
    904910                $valid_check = call_user_func( $arg['validate_callback'], $param, $this, $key );
    905911
     
    909915
    910916                if ( is_wp_error( $valid_check ) ) {
    911                     $invalid_params[ $key ] = $valid_check->get_error_message();
     917                    $invalid_params[ $key ]  = implode( ' ', $valid_check->get_error_messages() );
     918                    $invalid_details[ $key ] = rest_convert_error_to_response( $valid_check )->get_data();
    912919                }
    913920            }
     
    920927                sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ),
    921928                array(
    922                     'status' => 400,
    923                     'params' => $invalid_params,
     929                    'status'  => 400,
     930                    'params'  => $invalid_params,
     931                    'details' => $invalid_details,
    924932                )
    925933            );
Note: See TracChangeset for help on using the changeset viewer.