Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #10 from Zenith-Kim-Light/fix-binding-to-cloudeven…
Browse files Browse the repository at this point in the history
…t-type

Replaced throwing exceptions with logging and early exit
  • Loading branch information
mshumakov authored Jun 17, 2020
2 parents 5f6260b + f97331d commit 0d93993
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/Listeners/SubscriptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'];
Expand All @@ -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) {
Expand Down

0 comments on commit 0d93993

Please sign in to comment.