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.

Built from https://develop.svn.wordpress.org/trunk@35653


git-svn-id: http://core.svn.wordpress.org/trunk@35617 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Ryan McCue 2015-11-17 04:12:26 +00:00
parent 9524ebb38e
commit 81ffd2492c
2 changed files with 11 additions and 4 deletions

View File

@ -162,14 +162,21 @@ class WP_REST_Server {
$status = 500; $status = 500;
} }
$data = array(); $errors = array();
foreach ( (array) $error->errors as $code => $messages ) { foreach ( (array) $error->errors as $code => $messages ) {
foreach ( (array) $messages as $message ) { foreach ( (array) $messages as $message ) {
$data[] = array( 'code' => $code, 'message' => $message, 'data' => $error->get_error_data( $code ) ); $errors[] = array( 'code' => $code, 'message' => $message, 'data' => $error->get_error_data( $code ) );
} }
} }
$data = $errors[0];
if ( count( $errors ) > 1 ) {
// Remove the primary error.
array_shift( $errors );
$data['additional_errors'] = $errors;
}
$response = new WP_REST_Response( $data, $status ); $response = new WP_REST_Response( $data, $status );
return $response; return $response;
@ -198,7 +205,7 @@ class WP_REST_Server {
$error = compact( 'code', 'message' ); $error = compact( 'code', 'message' );
return wp_json_encode( array( $error ) ); return wp_json_encode( $error );
} }
/** /**

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.4-beta4-35652'; $wp_version = '4.4-beta4-35653';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.