Skip to content

Commit

Permalink
Merge pull request #90 from buggregator/hotfix/86
Browse files Browse the repository at this point in the history
Adds better message for invalid monolog message format.
  • Loading branch information
butschster authored Nov 10, 2023
2 parents 66a21e4 + da838b2 commit 511df49
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions app/modules/Monolog/Interfaces/TCP/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
namespace Modules\Monolog\Interfaces\TCP;

use App\Application\Commands\HandleReceivedEvent;
use Psr\Log\LoggerInterface;
use Spiral\Cqrs\CommandBusInterface;
use Spiral\Exceptions\ExceptionReporterInterface;
use Spiral\RoadRunner\Tcp\Request;
use Spiral\RoadRunner\Tcp\TcpEvent;
use Spiral\RoadRunnerBridge\Tcp\Response\CloseConnection;
use Spiral\RoadRunnerBridge\Tcp\Response\ContinueRead;
use Spiral\RoadRunnerBridge\Tcp\Response\ResponseInterface;
use Spiral\RoadRunnerBridge\Tcp\Service\ServiceInterface;

class Service implements ServiceInterface
final class Service implements ServiceInterface
{
public function __construct(
private readonly CommandBusInterface $commandBus,
private readonly LoggerInterface $logger,
) {
}

Expand All @@ -33,19 +36,20 @@ public function handle(Request $request): ResponseInterface
$messages = \array_filter(\explode("\n", $request->body));

foreach ($messages as $message) {
$payload = \json_decode($message, true);

// Impossible to decode the message, give up.
if (!$payload) {
throw new \RuntimeException("Unable to decode a message from [{$request->connectionUuid}] client.");
try {
$payload = \json_decode($message, true, JSON_THROW_ON_ERROR);
$this->commandBus->dispatch(
new HandleReceivedEvent(
type: 'monolog',
payload: $payload,
),
);
} catch (\JsonException $e) {
// Impossible to decode the message, give up.
$this->logger->error("Unable to decode log message. Should be a valid JSON.", [
'message' => $message,
]);
}

$this->commandBus->dispatch(
new HandleReceivedEvent(
type: 'monolog',
payload: $payload
)
);
}

return new ContinueRead();
Expand Down

0 comments on commit 511df49

Please sign in to comment.