Unverified Commit bf9d4031 authored by Valentine's avatar Valentine Committed by Stefan Auditor
Browse files

Issue #3300580 by vrs11, sanduhrs: Event on sending web hook error

parent 8dd3808f
Loading
Loading
Loading
Loading
+87 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\webhooks\Event;

use Drupal\webhooks\Entity\WebhookConfig;
use Drupal\webhooks\Webhook;
use Symfony\Component\EventDispatcher\Event;

/**
 * Class Send Event.
 *
 * @package Drupal\webhooks\Event
 */
class SendErrorEvent extends Event {

  /**
   * The webhook.
   *
   * @var \Drupal\webhooks\Webhook
   */
  protected $webhook;

  /**
   * The webhook configuration.
   *
   * @var \Drupal\webhooks\Entity\WebhookConfig
   */
  protected $webhookConfig;

  /**
   * The error.
   *
   * @var \Exception
   */
  protected $e;

  /**
   * SendEvent constructor.
   *
   * @param \Drupal\webhooks\Entity\WebhookConfig $webhook_config
   *   A webhook configuration entity.
   * @param \Drupal\webhooks\Webhook $webhook
   *   A webhook.
   * @param \Exception $e
   *   An error.
   */
  public function __construct(
      WebhookConfig $webhook_config,
      Webhook $webhook,
      \Exception $e
  ) {
    $this->webhookConfig = $webhook_config;
    $this->webhook = $webhook;
    $this->e = $e;
  }

  /**
   * Get the webhooks.
   *
   * @return \Drupal\webhooks\Webhook
   *   A webhook.
   */
  public function getWebhook() {
    return $this->webhook;
  }

  /**
   * Get the webhook configuration.
   *
   * @return \Drupal\webhooks\Entity\WebhookConfig
   *   A webhook configuration.
   */
  public function getWebhookConfig() {
    return $this->webhookConfig;
  }

  /**
   * Get the error.
   *
   * @return \Exception
   *   An error.
   */
  public function getError() {
    return $this->e;
  }

}
+12 −0
Original line number Diff line number Diff line
@@ -32,4 +32,16 @@ final class WebhookEvents {
   */
  const RECEIVE = 'webhook.receive';

  /**
   * Name of the event fired when a webhook send error is sent.
   *
   * This event allows modules to perform an action whenever a webhook send
   * error appears.
   * The event listener method receives a \Drupal\webhooks\Event\SendErrorEvent
   * instance.
   *
   * @Event
   */
  const SEND_ERROR = 'webhook.send.error';

}
+7 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ use Drupal\webhooks\Entity\WebhookConfig;
use Drupal\webhooks\Event\WebhookEvents;
use Drupal\webhooks\Event\ReceiveEvent;
use Drupal\webhooks\Event\SendEvent;
use Drupal\webhooks\Event\SendErrorEvent;
use Drupal\webhooks\Exception\WebhookIncomingEndpointNotFoundException;
use GuzzleHttp\Client;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -199,6 +200,12 @@ class WebhooksService implements WebhookDispatcherInterface, WebhookReceiverInte
      );
    }
    catch (\Exception $e) {
      // Dispatch Webhook Send error event.
      $this->eventDispatcher->dispatch(
        WebhookEvents::SEND_ERROR,
        new SendErrorEvent($webhook_config, $webhook, $e)
      );

      $this->logger->error(
        'Dispatch Failed. Subscriber %subscriber on Webhook %uuid for Event %event: @message', [
          '%subscriber' => $webhook_config->id(),