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

Improve error handling for trimming process #19

Merged
merged 2 commits into from
Oct 14, 2024
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
36 changes: 27 additions & 9 deletions classes/class-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,26 @@ final class Init extends PMC_WP_CLI {
* @return void
*/
public function start(): void {
WP_CLI::line( 'Starting local-data process.' );
try {
WP_CLI::line( 'Starting local-data process.' );

do_action( 'pmc_wp_cli_local_data_before_processing' );
do_action( 'pmc_wp_cli_local_data_before_processing' );

$this->_drop_table();
$this->_create_table();
$this->_drop_table();
$this->_create_table();

$this->_query_for_ids_to_keep();
$this->_query_for_ids_to_keep();

new Clean_DB();
new Clean_DB();

do_action( 'pmc_wp_cli_local_data_after_processing' );
do_action( 'pmc_wp_cli_local_data_after_processing' );

$this->_drop_table();
$this->_drop_table();

WP_CLI::line( 'Process complete.' );
WP_CLI::line( 'Process complete.' );
} catch ( Throwable $throwable ) {
$this->_handle_error( $throwable );
}
}

/**
Expand Down Expand Up @@ -230,4 +234,18 @@ private function _get_query_args_instances(): array {
$instance instanceof Query_Args
);
}

/**
* Handle errors during trimming process.
*
* @param Throwable $throwable Error details.
* @return void
*/
private function _handle_error( Throwable $throwable ): void {
global $wpdb;

$wpdb->query( 'DROP DATABASE ' . DB_NAME );

WP_CLI::error( $throwable->getMessage() );
}
}