Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors properly when WP_DISABLE_FATAL_ERROR_HANDLER is false #2789

Merged
merged 2 commits into from
May 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions includes/classes/IndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -728,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 );
}

/**
Expand Down Expand Up @@ -1125,7 +1138,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
*/
Expand All @@ -1135,6 +1148,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'];
Expand Down