<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[50150] trunk: REST API: Return detailed error information from request validation.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/50150">50150</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/50150","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>TimothyBlynJacobs</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-02-02 17:26:06 +0000 (Tue, 02 Feb 2021)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/46191">#46191</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiclasswprestrequestphp">trunk/src/wp-includes/rest-api/class-wp-rest-request.php</a></li>
<li><a href="#trunksrcwpincludesrestapiclasswprestserverphp">trunk/src/wp-includes/rest-api/class-wp-rest-server.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestattachmentscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestrequestphp">trunk/tests/phpunit/tests/rest-api/rest-request.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestserverphp">trunk/tests/phpunit/tests/rest-api/rest-server.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesrestapiclasswprestrequestphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/class-wp-rest-request.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/class-wp-rest-request.php 2021-02-02 17:21:45 UTC (rev 50149)
+++ trunk/src/wp-includes/rest-api/class-wp-rest-request.php 2021-02-02 17:26:06 UTC (rev 50150)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -802,7 +802,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $order = $this->get_parameter_order();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $invalid_params = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $invalid_params = array();
+ $invalid_details = array();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $order as $type ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $this->params[ $type ] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -825,10 +826,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /** @var mixed|WP_Error $sanitized_value */
</ins><span class="cx" style="display: block; padding: 0 10px"> $sanitized_value = call_user_func( $param_args['sanitize_callback'], $value, $this, $key );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $sanitized_value ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $invalid_params[ $key ] = $sanitized_value->get_error_message();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $invalid_params[ $key ] = implode( ' ', $sanitized_value->get_error_messages() );
+ $invalid_details[ $key ] = rest_convert_error_to_response( $sanitized_value )->get_data();
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->params[ $type ][ $key ] = $sanitized_value;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -841,8 +844,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: List of invalid parameters. */
</span><span class="cx" style="display: block; padding: 0 10px"> sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ),
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'status' => 400,
- 'params' => $invalid_params,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'status' => 400,
+ 'params' => $invalid_params,
+ 'details' => $invalid_details,
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -894,7 +898,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * This is done after required checking as required checking is cheaper.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $invalid_params = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $invalid_params = array();
+ $invalid_details = array();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $args as $key => $arg ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -901,6 +906,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $param = $this->get_param( $key );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( null !== $param && ! empty( $arg['validate_callback'] ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /** @var bool|\WP_Error $valid_check */
</ins><span class="cx" style="display: block; padding: 0 10px"> $valid_check = call_user_func( $arg['validate_callback'], $param, $this, $key );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( false === $valid_check ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -908,7 +914,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $valid_check ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $invalid_params[ $key ] = $valid_check->get_error_message();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $invalid_params[ $key ] = implode( ' ', $valid_check->get_error_messages() );
+ $invalid_details[ $key ] = rest_convert_error_to_response( $valid_check )->get_data();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -919,8 +926,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: List of invalid parameters. */
</span><span class="cx" style="display: block; padding: 0 10px"> sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ),
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'status' => 400,
- 'params' => $invalid_params,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'status' => 400,
+ 'params' => $invalid_params,
+ 'details' => $invalid_details,
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiclasswprestserverphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/class-wp-rest-server.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/class-wp-rest-server.php 2021-02-02 17:21:45 UTC (rev 50149)
+++ trunk/src/wp-includes/rest-api/class-wp-rest-server.php 2021-02-02 17:26:06 UTC (rev 50150)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -196,41 +196,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * list in JSON rather than an object/map.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.4.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.7.0 Converted to a wrapper of {@see rest_convert_error_to_response()}.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Error $error WP_Error instance.
</span><span class="cx" style="display: block; padding: 0 10px"> * @return WP_REST_Response List of associative arrays with code and message keys.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function error_to_response( $error ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $error_data = $error->get_error_data();
-
- if ( is_array( $error_data ) && isset( $error_data['status'] ) ) {
- $status = $error_data['status'];
- } else {
- $status = 500;
- }
-
- $errors = array();
-
- foreach ( (array) $error->errors as $code => $messages ) {
- foreach ( (array) $messages as $message ) {
- $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 );
-
- return $response;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return rest_convert_error_to_response( $error );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api.php 2021-02-02 17:21:45 UTC (rev 50149)
+++ trunk/src/wp-includes/rest-api.php 2021-02-02 17:26:06 UTC (rev 50150)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3182,3 +3182,57 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $endpoint_args;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+
+/**
+ * Converts an error to a response object.
+ *
+ * This iterates over all error codes and messages to change it into a flat
+ * array. This enables simpler client behaviour, as it is represented as a
+ * list in JSON rather than an object/map.
+ *
+ * @since 5.7.0
+ *
+ * @param WP_Error $error WP_Error instance.
+ *
+ * @return WP_REST_Response List of associative arrays with code and message keys.
+ */
+function rest_convert_error_to_response( $error ) {
+ $status = array_reduce(
+ $error->get_all_error_data(),
+ function ( $status, $error_data ) {
+ return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;
+ },
+ 500
+ );
+
+ $errors = array();
+
+ foreach ( (array) $error->errors as $code => $messages ) {
+ $all_data = $error->get_all_error_data( $code );
+ $last_data = array_pop( $all_data );
+
+ foreach ( (array) $messages as $message ) {
+ $formatted = array(
+ 'code' => $code,
+ 'message' => $message,
+ 'data' => $last_data,
+ );
+
+ if ( $all_data ) {
+ $formatted['additional_data'] = $all_data;
+ }
+
+ $errors[] = $formatted;
+ }
+ }
+
+ $data = $errors[0];
+ if ( count( $errors ) > 1 ) {
+ // Remove the primary error.
+ array_shift( $errors );
+ $data['additional_errors'] = $errors;
+ }
+
+ return new WP_REST_Response( $data, $status );
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsrestapirestattachmentscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php 2021-02-02 17:21:45 UTC (rev 50149)
+++ trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php 2021-02-02 17:26:06 UTC (rev 50150)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -455,9 +455,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'status', 'publish' );
</span><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'context', 'edit' );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $data = $response->get_data();
- $this->assertCount( 3, $data );
- $this->assertSame( 'rest_invalid_param', $data['code'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertErrorResponse( 'rest_invalid_param', $response );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_get_items_private_status() {
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestrequestphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-request.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-request.php 2021-02-02 17:21:45 UTC (rev 50149)
+++ trunk/tests/phpunit/tests/rest-api/rest-request.php 2021-02-02 17:26:06 UTC (rev 50150)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -462,6 +462,78 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'rest_invalid_param', $valid->get_error_code() );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @ticket 46191
+ */
+ public function test_sanitize_params_error_multiple_messages() {
+ $this->request->set_url_params(
+ array(
+ 'failparam' => '123',
+ )
+ );
+ $this->request->set_attributes(
+ array(
+ 'args' => array(
+ 'failparam' => array(
+ 'sanitize_callback' => function () {
+ $error = new WP_Error( 'invalid', 'Invalid.' );
+ $error->add( 'invalid', 'Super Invalid.' );
+ $error->add( 'broken', 'Broken.' );
+
+ return $error;
+ },
+ ),
+ ),
+ )
+ );
+
+ $valid = $this->request->sanitize_params();
+ $this->assertWPError( $valid );
+ $data = $valid->get_error_data();
+
+ $this->assertInternalType( 'array', $data );
+ $this->assertArrayHasKey( 'params', $data );
+ $this->assertArrayHasKey( 'failparam', $data['params'] );
+ $this->assertEquals( 'Invalid. Super Invalid. Broken.', $data['params']['failparam'] );
+ }
+
+ /**
+ * @ticket 46191
+ */
+ public function test_sanitize_params_provides_detailed_errors() {
+ $this->request->set_url_params(
+ array(
+ 'failparam' => '123',
+ )
+ );
+ $this->request->set_attributes(
+ array(
+ 'args' => array(
+ 'failparam' => array(
+ 'sanitize_callback' => function () {
+ return new WP_Error( 'invalid', 'Invalid.', 'mydata' );
+ },
+ ),
+ ),
+ )
+ );
+
+ $valid = $this->request->sanitize_params();
+ $this->assertWPError( $valid );
+
+ $data = $valid->get_error_data();
+ $this->assertArrayHasKey( 'details', $data );
+ $this->assertArrayHasKey( 'failparam', $data['details'] );
+ $this->assertEquals(
+ array(
+ 'code' => 'invalid',
+ 'message' => 'Invalid.',
+ 'data' => 'mydata',
+ ),
+ $data['details']['failparam']
+ );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_sanitize_params_with_null_callback() {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->request->set_url_params(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -652,6 +724,79 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'This is not valid!', $error_data['params']['someotherparams'] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @ticket 46191
+ */
+ public function test_invalid_params_error_multiple_messages() {
+ $this->request->set_url_params(
+ array(
+ 'failparam' => '123',
+ )
+ );
+ $this->request->set_attributes(
+ array(
+ 'args' => array(
+ 'failparam' => array(
+ 'validate_callback' => function () {
+ $error = new WP_Error( 'invalid', 'Invalid.' );
+ $error->add( 'invalid', 'Super Invalid.' );
+ $error->add( 'broken', 'Broken.' );
+
+ return $error;
+ },
+ ),
+ ),
+ )
+ );
+
+ $valid = $this->request->has_valid_params();
+ $this->assertWPError( $valid );
+ $data = $valid->get_error_data();
+
+ $this->assertInternalType( 'array', $data );
+ $this->assertArrayHasKey( 'params', $data );
+ $this->assertArrayHasKey( 'failparam', $data['params'] );
+ $this->assertEquals( 'Invalid. Super Invalid. Broken.', $data['params']['failparam'] );
+ }
+
+ /**
+ * @ticket 46191
+ */
+ public function test_invalid_params_provides_detailed_errors() {
+ $this->request->set_url_params(
+ array(
+ 'failparam' => '123',
+ )
+ );
+ $this->request->set_attributes(
+ array(
+ 'args' => array(
+ 'failparam' => array(
+ 'validate_callback' => function () {
+ return new WP_Error( 'invalid', 'Invalid.', 'mydata' );
+ },
+ ),
+ ),
+ )
+ );
+
+ $valid = $this->request->has_valid_params();
+ $this->assertWPError( $valid );
+
+ $data = $valid->get_error_data();
+ $this->assertArrayHasKey( 'details', $data );
+ $this->assertArrayHasKey( 'failparam', $data['details'] );
+ $this->assertEquals(
+ array(
+ 'code' => 'invalid',
+ 'message' => 'Invalid.',
+ 'data' => 'mydata',
+ ),
+ $data['details']['failparam']
+ );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function _return_wp_error_on_validate_callback() {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'some-error', 'This is not valid!' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestserverphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-server.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-server.php 2021-02-02 17:21:45 UTC (rev 50149)
+++ trunk/tests/phpunit/tests/rest-api/rest-server.php 2021-02-02 17:26:06 UTC (rev 50150)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -407,7 +407,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $message = 'Test error message for the API';
</span><span class="cx" style="display: block; padding: 0 10px"> $error = new WP_Error( $code, $message );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $response = rest_get_server()->error_to_response( $error );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $response = rest_convert_error_to_response( $error );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertInstanceOf( 'WP_REST_Response', $response );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Make sure we default to a 500 error.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -424,7 +424,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $message = 'Test error message for the API';
</span><span class="cx" style="display: block; padding: 0 10px"> $error = new WP_Error( $code, $message, array( 'status' => 400 ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $response = rest_get_server()->error_to_response( $error );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $response = rest_convert_error_to_response( $error );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertInstanceOf( 'WP_REST_Response', $response );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 400, $response->get_status() );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -443,7 +443,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $error = new WP_Error( $code, $message, array( 'status' => 400 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $error->add( $code2, $message2, array( 'status' => 403 ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $response = rest_get_server()->error_to_response( $error );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $response = rest_convert_error_to_response( $error );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertInstanceOf( 'WP_REST_Response', $response );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 400, $response->get_status() );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -456,6 +456,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( array( 'status' => 403 ), $error->error_data[ $code2 ] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @ticket 46191
+ */
+ public function test_error_to_response_with_additional_data() {
+ $error = new WP_Error( 'test', 'test', array( 'status' => 400 ) );
+ $error->add_data( 'more_data' );
+
+ $response = rest_convert_error_to_response( $error );
+ $this->assertSame( 400, $response->get_status() );
+ $this->assertEquals( 'more_data', $response->get_data()['data'] );
+ $this->assertEquals( array( array( 'status' => 400 ) ), $response->get_data()['additional_data'] );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_rest_error() {
</span><span class="cx" style="display: block; padding: 0 10px"> $data = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'code' => 'wp-api-test-error',
</span></span></pre>
</div>
</div>
</body>
</html>