Is there a way to force a batch process to stop if some kind of error occurs? In particular, I have a batch that starts with saving a record to a database. Each subsequent batch operation depends on the database insert working correctly. I'd like to force the batch to stop if the first batch fails.
The batch is called like this
$batch = array(
'operations' => $operations,
'finished' => 'mymodule_batch_finished',
'title' => t('Merging records'),
'init_message' => t('Starting processing'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Processing has encountered an error'),
'file' => drupal_get_path('module', 'mymodule') . '/forms/mymodule.inc'
);
where $operations
is the array of functions to call.
Each function uses PDO to update the database. I'm trying to figure out how to stop the subsequent $operations
from being called if the query in the current operation fails.
try {
$sth = $dbh->prepare($sql);
$sth->bindParam(':newid',$new_id,PDO::PARAM_INT);
$sth->bindParam(':id',$id,PDO::PARAM_INT);
if($sth->execute()) {
$context['results']['[] = $sth->rowCount();
}
else {
// TODO: stop processing due to an error
}
}
catch(PDOException $e) {
// TODO: stop processing due to an error
}
If an error occurs, I want to bypass the remaining operations and go straight to mymodule_batch_finished