Skip to content

Commit

Permalink
fix: add catch to any throwable in consumer manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ssgoncalves committed Oct 10, 2022
1 parent 5d0d65a commit 056dba7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/Connectors/Consumer/Manager.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
namespace Metamorphosis\Connectors\Consumer;

use Exception;
use Metamorphosis\Consumers\ConsumerInterface;
use Metamorphosis\Exceptions\ResponseTimeoutException;
use Metamorphosis\Exceptions\ResponseWarningException;
use Metamorphosis\Middlewares\Handler\Dispatcher;
use Metamorphosis\Record\ConsumerRecord;
use Metamorphosis\TopicHandler\Consumer\Handler as ConsumerHandler;
use RdKafka\Message;
use Throwable;

class Manager
{
Expand Down Expand Up @@ -79,8 +79,8 @@ public function handleMessage(): void
} catch (ResponseWarningException $exception) {
$this->consumerHandler->warning($exception);
return;
} catch (Exception $exception) {
$this->consumerHandler->failed($exception);
} catch (Throwable $throwable) {
$this->consumerHandler->failed($throwable);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/TopicHandler/Consumer/AbstractHandler.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace Metamorphosis\TopicHandler\Consumer;

use Exception;
use Metamorphosis\Exceptions\ResponseWarningException;
use Metamorphosis\TopicHandler\ConfigOptions\Consumer as ConsumerConfigOptions;
use Throwable;

abstract class AbstractHandler implements Handler
{
Expand All @@ -23,7 +23,7 @@ public function warning(ResponseWarningException $exception): void
{
}

public function failed(Exception $exception): void
public function failed(Throwable $throwable): void
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/TopicHandler/Consumer/Handler.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace Metamorphosis\TopicHandler\Consumer;

use Exception;
use Metamorphosis\Exceptions\ResponseWarningException;
use Metamorphosis\Record\RecordInterface;
use Throwable;

interface Handler
{
Expand All @@ -25,5 +25,5 @@ public function finished(): void;
/**
* Handle failure process.
*/
public function failed(Exception $exception): void;
public function failed(Throwable $throwable): void;
}
6 changes: 3 additions & 3 deletions tests/Integration/Dummies/MessageConsumer.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace Tests\Integration\Dummies;

use Exception;
use Illuminate\Support\Facades\Log;
use Metamorphosis\Exceptions\ResponseWarningException;
use Metamorphosis\Record\RecordInterface;
use Metamorphosis\TopicHandler\Consumer\AbstractHandler;
use Throwable;

class MessageConsumer extends AbstractHandler
{
Expand All @@ -23,10 +23,10 @@ public function warning(ResponseWarningException $exception): void
]);
}

public function failed(Exception $exception): void
public function failed(Throwable $throwable): void
{
Log::error('Failed to handle kafka record for sku.', [
'exception' => $exception,
'exception' => $throwable,
]);
}
}
24 changes: 24 additions & 0 deletions tests/Unit/Connectors/Consumer/ManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Tests\Unit\Connectors\Consumer;

use Error;
use Exception;
use Metamorphosis\Connectors\Consumer\Manager;
use Metamorphosis\ConsumerConfigManager;
Expand Down Expand Up @@ -35,6 +36,29 @@ protected function setUp(): void
]);
}

public function testShouldErrorHandleWithError(): void
{
// Set
$consumer = m::mock(ConsumerInterface::class);
$consumerHandler = m::mock(ConsumerHandler::class);
$dispatcher = m::mock(Dispatcher::class);

$runner = new Manager($consumer, $consumerHandler, $dispatcher, true, false);

$error = new Error('Error occurs when consuming.');

// Expectations
$consumer->expects()
->consume()
->andThrow($error);

$consumerHandler->expects()
->failed($error);

// Actions
$runner->handleMessage();
}

public function testShouldHandleMultiplesMessages(): void
{
// Set
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Dummies/ConsumerHandlerDummy.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php
namespace Tests\Unit\Dummies;

use Exception;
use Metamorphosis\Record\RecordInterface;
use Metamorphosis\TopicHandler\Consumer\AbstractHandler;
use Throwable;

class ConsumerHandlerDummy extends AbstractHandler
{
public function handle(RecordInterface $data): void
{
}

public function failed(Exception $exception): void
public function failed(Throwable $throwable): void
{
}
}

0 comments on commit 056dba7

Please sign in to comment.