From f97331d9f43ddfc9949d73be18eb9080bd5e682e Mon Sep 17 00:00:00 2001 From: kim_light Date: Tue, 16 Jun 2020 17:00:32 +0300 Subject: [PATCH] Replaced throwing exceptions with logging and early exit --- src/Listeners/SubscriptionListener.php | 28 +++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Listeners/SubscriptionListener.php b/src/Listeners/SubscriptionListener.php index f2eb903..5d745e1 100644 --- a/src/Listeners/SubscriptionListener.php +++ b/src/Listeners/SubscriptionListener.php @@ -6,14 +6,14 @@ use LeNats\Contracts\EventDispatcherAwareInterface; use LeNats\Events\CloudEvent; use LeNats\Events\Nats\SubscriptionMessageReceived; -use LeNats\Exceptions\ConnectionException; -use LeNats\Exceptions\StreamException; use LeNats\Exceptions\SubscriptionException; use LeNats\Services\EventTypeResolver; use LeNats\Subscription\Subscriber; use LeNats\Support\Dispatcherable; use NatsStreamingProtocol\MsgProto; use LeNats\Events\Fake\CloudEvent as FakeCloudEvent; +use Psr\Log\LoggerInterface; +use Throwable; class SubscriptionListener implements EventDispatcherAwareInterface { @@ -34,35 +34,43 @@ class SubscriptionListener implements EventDispatcherAwareInterface */ private $subscriber; + /** + * @var \Psr\Log\LoggerInterface + */ + private $logger; + public function __construct( SerializerInterface $serializer, EventTypeResolver $typeResolver, - Subscriber $subscriber + Subscriber $subscriber, + LoggerInterface $logger ) { $this->serializer = $serializer; $this->typeResolver = $typeResolver; $this->subscriber = $subscriber; + $this->logger = $logger; } /** - * @param SubscriptionMessageReceived $event + * @param SubscriptionMessageReceived $event + * * @throws SubscriptionException - * @throws StreamException - * @throws ConnectionException */ public function handle(SubscriptionMessageReceived $event): void { $message = new MsgProto(); try { $message->mergeFromString($event->payload); - } catch (\Throwable $e) { + } catch (Throwable $e) { throw new SubscriptionException($e->getMessage()); } $data = json_decode($message->getData(), true); if (!array_key_exists('type', $data)) { - throw new SubscriptionException('Event type not found'); + $this->logger->error('Event type not found'); + + return; } $eventType = $data['type']; @@ -78,7 +86,9 @@ public function handle(SubscriptionMessageReceived $event): void $cloudEvent = $this->serializer->deserialize($message->getData(), $eventClass, 'json'); if (!($cloudEvent instanceof CloudEvent) && !($cloudEvent instanceof FakeCloudEvent)) { - throw new SubscriptionException($eventClass . ' must be instance of CloudEvent'); + $this->logger->error($eventClass . ' must be instance of CloudEvent'); + + return; } if ($cloudEvent instanceof FakeCloudEvent) {