From f29ee77ff4f321df38cd4cff0e7f06d8a8a47c1f Mon Sep 17 00:00:00 2001
From: Rosie Le Faive <lefaive@gmail.com>
Date: Tue, 13 Dec 2022 16:43:06 -0400
Subject: [PATCH] Use a proper exception.

---
 src/EventGenerator/EmitEvent.php                 |  9 +++++----
 src/Exception/IslandoraDerivativeException.php   | 12 ++++++++++++
 src/Plugin/Action/AbstractGenerateDerivative.php |  3 ++-
 3 files changed, 19 insertions(+), 5 deletions(-)
 create mode 100644 src/Exception/IslandoraDerivativeException.php

diff --git a/src/EventGenerator/EmitEvent.php b/src/EventGenerator/EmitEvent.php
index 2e0d226e..fd33fd99 100644
--- a/src/EventGenerator/EmitEvent.php
+++ b/src/EventGenerator/EmitEvent.php
@@ -14,6 +14,7 @@ use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\StringTranslation\StringTranslationTrait;
 use Drupal\islandora\Event\StompHeaderEvent;
 use Drupal\islandora\Event\StompHeaderEventException;
+use Drupal\islandora\Exception\IslandoraDerivativeException;
 use Stomp\Exception\StompException;
 use Stomp\StatefulStomp;
 use Stomp\Transport\Message;
@@ -157,10 +158,6 @@ abstract class EmitEvent extends ConfigurableActionBase implements ContainerFact
 
       $user = $this->entityTypeManager->getStorage('user')->load($this->account->id());
       $data = $this->generateData($entity);
-      // If $data is the bool false, then abort. No error, but don't emit event.
-      if ($data === FALSE) {
-        return;
-      }
 
       $event = $this->eventDispatcher->dispatch(
         StompHeaderEvent::EVENT_NAME,
@@ -172,6 +169,10 @@ abstract class EmitEvent extends ConfigurableActionBase implements ContainerFact
         $event->getHeaders()->all()
       );
     }
+    catch (IslandoraDerivativeException $e) {
+      $this->logger->info($e->getMessage());
+      return;
+    }
     catch (StompHeaderEventException $e) {
       $this->logger->error($e->getMessage());
       $this->messenger->addError($e->getMessage());
diff --git a/src/Exception/IslandoraDerivativeException.php b/src/Exception/IslandoraDerivativeException.php
new file mode 100644
index 00000000..66f63606
--- /dev/null
+++ b/src/Exception/IslandoraDerivativeException.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace Drupal\islandora\Exception;
+
+/**
+ * Islandora exceptions.
+ *
+ * @package islandora
+ */
+class IslandoraDerivativeException extends \RuntimeException
+{
+}
diff --git a/src/Plugin/Action/AbstractGenerateDerivative.php b/src/Plugin/Action/AbstractGenerateDerivative.php
index 129af994..339d1d87 100644
--- a/src/Plugin/Action/AbstractGenerateDerivative.php
+++ b/src/Plugin/Action/AbstractGenerateDerivative.php
@@ -5,6 +5,7 @@ namespace Drupal\islandora\Plugin\Action;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Url;
+use Drupal\islandora\Exception\IslandoraDerivativeException;
 
 /**
  * Emits a Node event.
@@ -64,7 +65,7 @@ class AbstractGenerateDerivative extends AbstractGenerateDerivativeBase {
     // same as the source media. Dont cause an error, just don't continue.
     $derivative_media = $this->utils->getMediaWithTerm($entity, $derivative_term);
     if (!is_null($derivative_media) && $derivative_media->id() == $source_media->id()) {
-      return FALSE;
+      throw new IslandoraDerivativeException("Halting derivative, as source and target media are the same. Derivative term: [" . $this->configuration['derivative_term_uri'] . "] Source term: [" . $this->configuration['source_term_uri'] . "] Node id: [". $entity->id() . "].",  500);
     }
 
     $route_params = [