0

I have some special configured content entities in my system which are protected in a pre-delete hook against entity deletion. There is also a batch process (VBO - entity delete) where they can be selected before hand.

I want to handle gracefully the exception in a batch process which wasn't written by me (and cannot be touched). I tried to subscribe to the exception event and look for xmlHttpRequest which was all fine, but then I dunno what response to set back to stop the process, or log the exception within the batch process with the proper message. What I always get back is this, where the error message is what I put into the AjaxResponse body:

enter image description here

Any help appreciated find a more sophisticated solution to redirect from the batch processing in case of an exception.

8
  • You need to return an ajax response. But solving the problem how you can pick up the batch once you've thrown an exception seems to be far more complicated than a simple try/catch statement around the delete in the batch process, so that you can pick up the deletion process right away in the same batch request without bothering the user.
    – 4k4
    Jun 7 '19 at 6:31
  • But this is what am currently doing: returning an ajax response and I also attached the image above what the result is. This is not working as you can see.
    – ssibal
    Jun 7 '19 at 6:35
  • And I also wrote: it's a 3rd party code, I don't even have the chance to put try catch block anywhere. Please read carefully again.
    – ssibal
    Jun 7 '19 at 6:41
  • You would need to return an Ajax response which pickups the batch process, not a 500 status code. I've read the question. I didn't try to answer. My comment was saying, look you could add a simple try/catch to the batch or you have the very difficult task how to pickup the batch again once you are thrown out.
    – 4k4
    Jun 7 '19 at 6:46
  • I find this unbelievable that the batch API cannot take care of simple exceptions.
    – ssibal
    Jun 7 '19 at 6:55
3

So in the end I took and overrode the plugin provided by the VBO. This will put the exception message into the summary displayed with a drupal_set_message at the end of the batch process (VBO 3.0.0-alpha2).

namespace Drupal\my_module\Plugin\Action;

use Drupal\views_bulk_operations\Plugin\Action\EntityDeleteAction;

/**
 * Delete entity action with default confirmation form.
 *
 * @Action(
 *   id = "my_module_delete_entity",
 *   label = @Translation("MyModule Delete selected entities"),
 *   type = "",
 *   confirm = TRUE,
 * )
 */
class MyModuleEntityDeleteAction extends EntityDeleteAction {

  public function execute($entity = NULL) {
    try {
      $entity->delete();
      return $this->t('Delete entities');
    }
    catch (\Exception $e) {
      return $e->getMessage();
    }
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.