From c1c52f36e30fb40ddf629de1d8cfe6d0bd6c8ac9 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 23 May 2022 09:18:56 -0300 Subject: [PATCH 1/2] Handle errors properly when WP_DISABLE_FATAL_ERROR_HANDLER is false (the default) --- includes/classes/IndexHelper.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/includes/classes/IndexHelper.php b/includes/classes/IndexHelper.php index f7cd78cd48..9b182570e2 100644 --- a/includes/classes/IndexHelper.php +++ b/includes/classes/IndexHelper.php @@ -66,6 +66,7 @@ public function setup() { */ public function full_index( $args ) { register_shutdown_function( [ $this, 'handle_index_error' ] ); + add_filter( 'wp_php_error_message', [ $this, 'wp_handle_index_error' ], 10, 2 ); $this->index_meta = Utils\get_indexing_status(); $this->args = $args; @@ -1125,7 +1126,7 @@ public function get_index_meta() { /** * Handle fatal errors during syncs. * - * Logs the error and clears the sync status, preventing the sync status from being stuck. + * Added by register_shutdown_function. It will not be called if `WP_DISABLE_FATAL_ERROR_HANDLER` is false (default.) * * @since 4.2.0 */ @@ -1135,6 +1136,31 @@ public function handle_index_error() { return; } + $this->on_error_update_and_clean( $error ); + } + + /** + * Handle fatal errors during syncs. + * + * Added via the `wp_php_error_message` filter. It will be called only if `WP_DISABLE_FATAL_ERROR_HANDLER` is false (default.) + * + * @since 4.2.0 + * @param bool $message HTML error message to display. + * @param array $error Error information retrieved from error_get_last(). + * @return bool + */ + public function wp_handle_index_error( $message, $error ) { + $this->on_error_update_and_clean( $error ); + return $message; + } + + /** + * Logs the error and clears the sync status, preventing the sync status from being stuck. + * + * @since 4.2.0 + * @param array $error Error information retrieved from error_get_last(). + */ + protected function on_error_update_and_clean( $error ) { $this->update_totals_from_current_sync_item(); $totals = $this->index_meta['totals']; From 86de9077f2a6afb18ac4e35055d6073d4c30f5de Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 23 May 2022 09:50:52 -0300 Subject: [PATCH 2/2] Limit the number of errors logged/stored. --- includes/classes/IndexHelper.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/includes/classes/IndexHelper.php b/includes/classes/IndexHelper.php index 9b182570e2..2d3fa1e85d 100644 --- a/includes/classes/IndexHelper.php +++ b/includes/classes/IndexHelper.php @@ -729,14 +729,26 @@ function( $item ) { protected function update_totals_from_current_sync_item() { $current_sync_item = $this->index_meta['current_sync_item']; + $errors = array_merge( + $this->index_meta['totals']['errors'], + $current_sync_item['errors'] + ); + + /** + * Filter the number of errors of a sync that should be stored. + * + * @since 4.2.0 + * @hook ep_sync_number_of_errors_stored + * @param {int} $number Number of errors to be logged. + * @return {int} New value + */ + $logged_errors = (int) apply_filters( 'ep_sync_number_of_errors_stored', 50 ); + $this->index_meta['totals']['total'] += $current_sync_item['total']; $this->index_meta['totals']['synced'] += $current_sync_item['synced']; $this->index_meta['totals']['skipped'] += $current_sync_item['skipped']; $this->index_meta['totals']['failed'] += $current_sync_item['failed']; - $this->index_meta['totals']['errors'] = array_merge( - $this->index_meta['totals']['errors'], - $current_sync_item['errors'] - ); + $this->index_meta['totals']['errors'] = array_slice( $errors, $logged_errors * -1 ); } /**