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/src/wp-includes/rest-api.php

    r50065 r50150  
    31833183    return $endpoint_args;
    31843184}
     3185
     3186
     3187/**
     3188 * Converts an error to a response object.
     3189 *
     3190 * This iterates over all error codes and messages to change it into a flat
     3191 * array. This enables simpler client behaviour, as it is represented as a
     3192 * list in JSON rather than an object/map.
     3193 *
     3194 * @since 5.7.0
     3195 *
     3196 * @param WP_Error $error WP_Error instance.
     3197 *
     3198 * @return WP_REST_Response List of associative arrays with code and message keys.
     3199 */
     3200function rest_convert_error_to_response( $error ) {
     3201    $status = array_reduce(
     3202        $error->get_all_error_data(),
     3203        function ( $status, $error_data ) {
     3204            return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;
     3205        },
     3206        500
     3207    );
     3208
     3209    $errors = array();
     3210
     3211    foreach ( (array) $error->errors as $code => $messages ) {
     3212        $all_data  = $error->get_all_error_data( $code );
     3213        $last_data = array_pop( $all_data );
     3214
     3215        foreach ( (array) $messages as $message ) {
     3216            $formatted = array(
     3217                'code'    => $code,
     3218                'message' => $message,
     3219                'data'    => $last_data,
     3220            );
     3221
     3222            if ( $all_data ) {
     3223                $formatted['additional_data'] = $all_data;
     3224            }
     3225
     3226            $errors[] = $formatted;
     3227        }
     3228    }
     3229
     3230    $data = $errors[0];
     3231    if ( count( $errors ) > 1 ) {
     3232        // Remove the primary error.
     3233        array_shift( $errors );
     3234        $data['additional_errors'] = $errors;
     3235    }
     3236
     3237    return new WP_REST_Response( $data, $status );
     3238}
Note: See TracChangeset for help on using the changeset viewer.