In function batch_example_finished(), if an error occurs during batch processing, the callback function reports the arguments array of the first remaining operation. The wrong index of $error_operation is used.

The line

          '@args' => print_r($error_operation[0], TRUE),

Should be

          '@args' => print_r($error_operation[1], TRUE),

A minor one, but new developers may easily reproduce it in their code.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Wtower’s picture

Status: Active » Needs review
FileSize
573 bytes

Patch included.

Mile23’s picture

I'm not sure why it's wrong. The difference between 0 and 1 is here, from batch_example_batch_1():

array(
  $nid,
  t('(Operation @operation)', array('@operation' => $i)),
),

So ostensibly your patch will show the operation text whereas currently it shows the $nid.

Unless I'm completely off the mark...

It would be great to have a test that demonstrates this. Maybe create a node, call batch_example_batch_1() to load up the batch, then delete the node to get an expected error when the batch is run.

Mile23’s picture

Status: Needs review » Needs work
Wtower’s picture

$error_operation in batch_example_finished() is the first element of the remaining $operations in case of error:

    $error_operation = reset($operations);

$operations is an array defined in batch_example_batch_1() and batch_example_batch_2(). A look at $error_operation content from batch 1 if I trigger an error on the very first operation:

array (
  0 => 'batch_example_op_1',
  1 => 
  array (
    0 => '1',
    1 => '(Operation 0)',
  ),
)

And from batch 2:

array (
  0 => 'batch_example_op_2',
  1 => 
  array (
    0 => '(Operation 0)',
  ),
)

In case of error, batch_example_finished() gives the followng message:

    drupal_set_message(
      t('An error occurred while processing @operation with arguments : @args',
        array(
          '@operation' => $error_operation[0],
          '@args' => print_r($error_operation[0], TRUE),
        )
      )
    );

So it will be: "An error occurred while processing batch_example_op_1 with arguments : batch_example_op_1" whereas I believe @args should display a dump of the second element of the array which is Node ID and 'Operation 0' for batch 1 or 'Operation 0' for batch 2.

jungle’s picture

Status: Needs work » Closed (outdated)

No activity for over 9 years. I am closing this. Please feel free to reopen if necessary.

Thanks!