Repository: 10up/ElasticPress (PHP)

Issue:
Create a new --stop-on-errors parameter for the WP-CLI index command
**Is your enhancement related to a problem? Please describe.**

Currently, WP-CLI index command will only stop if the response to the request is an error. Otherwise, it will record all errors (like "too many fields" errors) and present them at the end of the whole process.

**Describe the solution you'd like**

It would be nice to have a way to stop everything on the first sign of a problem. 

**Additional context**
Related lines of code:
Line where the command will stop if the response is an error: https://github.com/10up/ElasticPress/blob/develop/includes/classes/Command.php#L889
Line where the command will add the "soft errors" to an array:
https://github.com/10up/ElasticPress/blob/develop/includes/classes/Command.php#L917
Line where all errors are finally displayed:
https://github.com/10up/ElasticPress/blob/develop/includes/classes/Command.php#L946



Fix (patch):
diff --git a/includes/classes/Command.php b/includes/classes/Command.php
index 7b2f20e2ed..e96872a18d 100644
--- a/includes/classes/Command.php
+++ b/includes/classes/Command.php
@@ -689,6 +689,9 @@ public function delete_transient_on_int( $signal_no ) {
 	 * [--show-nobulk-errors]
 	 * : Display the error message returned from Elasticsearch when a post fails to index while not using the /_bulk endpoint
 	 *
+	 * [--stop-on-error]
+	 * : Stop indexing if an error is encountered and display the error.
+	 *
 	 * [--offset=<offset_number>]
 	 * : Skip the first n posts (don't forget to remove the `--setup` flag when resuming or the index will be emptied before starting again).
 	 *
@@ -800,7 +803,14 @@ public function sync( $args, $assoc_args ) {
 			'static_bulk'    => $static_bulk,
 		];
 
-		if ( isset( $assoc_args['show-errors'] ) || ( isset( $assoc_args['show-bulk-errors'] ) && ! $no_bulk ) || ( isset( $assoc_args['show-nobulk-errors'] ) && $no_bulk ) ) {
+		$index_args['stop_on_error'] = WP_CLI\Utils\get_flag_value( $assoc_args, 'stop-on-error', false );
+
+		$show_errors = $index_args['stop_on_error'] ||
+			WP_CLI\Utils\get_flag_value( $assoc_args, 'show-errors', false ) ||
+			( WP_CLI\Utils\get_flag_value( $assoc_args, 'show-bulk-errors', false ) && ! $no_bulk ) ||
+			( WP_CLI\Utils\get_flag_value( $assoc_args, 'show-nobulk-errors', false ) && $no_bulk );
+
+		if ( $show_errors ) {
 			$index_args['show_errors'] = true;
 		}
 
@@ -1310,6 +1320,7 @@ public function index_output( $message, $args, $index_meta, $context ) {
 				if ( empty( $args['show_errors'] ) ) {
 					return;
 				}
+
 				WP_CLI::warning( $message['message'] );
 				break;
 
diff --git a/includes/classes/IndexHelper.php b/includes/classes/IndexHelper.php
index 3dfa1b62c8..0f20791cc9 100644
--- a/includes/classes/IndexHelper.php
+++ b/includes/classes/IndexHelper.php
@@ -716,8 +716,9 @@ function( $item ) {
 				);
 
 				$this->index_meta['current_sync_item']['failed'] += count( $failed_objects );
+				$error_type                                       = ! empty( $this->args['stop_on_error'] ) ? 'error' : 'warning';
 
-				$this->queue_message( $errors_output, 'warning' );
+				$this->queue_message( $errors_output, $error_type );
 			} else {
 				$this->index_meta['current_sync_item']['synced'] += count( $queued_items );
 			}