Make WordPress Core

Changeset 35653


Ignore:
Timestamp:
11/17/2015 04:11:21 AM (9 years ago)
Author:
rmccue
Message:

REST API: Optimise for singular error instances.

Previously, the API returned a list of errors, as WP_Error can hold multiple
error codes internally. This isn't a particularly common use case, and it
makes handling errors on the client side more complex than it needs to be.

Fixes #34551.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r35652 r35653  
    163163        }
    164164
    165         $data = array();
     165        $errors = array();
    166166
    167167        foreach ( (array) $error->errors as $code => $messages ) {
    168168            foreach ( (array) $messages as $message ) {
    169                 $data[] = array( 'code' => $code, 'message' => $message, 'data' => $error->get_error_data( $code ) );
    170             }
     169                $errors[] = array( 'code' => $code, 'message' => $message, 'data' => $error->get_error_data( $code ) );
     170            }
     171        }
     172
     173        $data = $errors[0];
     174        if ( count( $errors ) > 1 ) {
     175            // Remove the primary error.
     176            array_shift( $errors );
     177            $data['additional_errors'] = $errors;
    171178        }
    172179
     
    199206        $error = compact( 'code', 'message' );
    200207
    201         return wp_json_encode( array( $error ) );
     208        return wp_json_encode( $error );
    202209    }
    203210
Note: See TracChangeset for help on using the changeset viewer.