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 e53b1ec
Show file tree
Hide file tree
Showing 6 changed files with 58 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,
]);
}
}
46 changes: 46 additions & 0 deletions tests/Unit/Connectors/Consumer/ManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace Tests\Unit\Connectors\Consumer;

use Error;
use Exception;
use InvalidArgumentException;
use Metamorphosis\Connectors\Consumer\Manager;
use Metamorphosis\ConsumerConfigManager;
use Metamorphosis\Consumers\ConsumerInterface;
Expand All @@ -14,6 +16,8 @@
use RdKafka\Message as KafkaMessage;
use Tests\LaravelTestCase;
use Tests\Unit\Dummies\ConsumerHandlerDummy;
use Throwable;
use TypeError;

class ManagerTest extends LaravelTestCase
{
Expand All @@ -35,6 +39,30 @@ protected function setUp(): void
]);
}

/**
* @dataProvider getThrowableScenarios
*/
public function testShouldHandlerAnyThrowable(Throwable $throwable): 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);

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

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

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

public function testShouldHandleMultiplesMessages(): void
{
// Set
Expand Down Expand Up @@ -171,4 +199,22 @@ public function testShouldHandleAsyncCommit(): void
$runner->handleMessage();
$runner->handleMessage();
}

public function getThrowableScenarios(): array
{
return [
'Exception' => [
'throwable' => new Exception(),
],
'Error' => [
'throwable' => new Error(),
],
'InvalidArgumentException' => [
'throwable' => new InvalidArgumentException(),
],
'TypeError' => [
'throwable' => new TypeError(),
],
];
}
}
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 e53b1ec

Please sign in to comment.