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.


git-svn-id: https://develop.svn.wordpress.org/trunk@35653 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan McCue 2015-11-17 04:11:21 +00:00
parent 10ecf65a4a
commit a6aa1a0754

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 );
} }
/** /**