Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store projection error objects #436

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions src/Console/Command/ProjectionStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Patchlevel\EventSourcing\Console\Command;

use Patchlevel\EventSourcing\Console\InputHelper;
use Patchlevel\EventSourcing\Console\OutputStyle;
use Patchlevel\EventSourcing\Projection\Projection\Projection;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionId;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

use function array_map;

Expand All @@ -18,31 +22,79 @@
)]
final class ProjectionStatusCommand extends ProjectionCommand
{
protected function configure(): void
{
parent::configure();

$this->addArgument(
'id',
InputArgument::OPTIONAL,
'The projection to display more information about',
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new OutputStyle($input, $output);

$id = InputHelper::nullableString($input->getArgument('id'));
$projections = $this->projectionist->projections();

$io->table(
if ($id === null) {
$io->table(
[
'id',
'name',
'version',
'position',
'status',
'error message',
],
array_map(
static fn (Projection $projection) => [
$projection->id()->toString(),
$projection->id()->name(),
$projection->id()->version(),
$projection->position(),
$projection->status()->value,
$projection->errorMessage(),
],
[...$projections],
),
);

return 0;
}

$projection = $projections->get(ProjectionId::fromString($id));

$io->horizontalTable(
[
'id',
'name',
'version',
'position',
'status',
'error message',
],
array_map(
static fn (Projection $projection) => [
[
[
$projection->id()->toString(),
$projection->id()->name(),
$projection->id()->version(),
$projection->position(),
$projection->status()->value,
$projection->errorMessage(),
],
[...$projections],
),
],
);

$errorObject = $projection->errorObject();

if ($errorObject instanceof Throwable) {
$io->throwable($errorObject);
}

return 0;
}
}
16 changes: 16 additions & 0 deletions src/Console/OutputStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Patchlevel\EventSourcing\Serializer\Encoder\Encoder;
use Patchlevel\EventSourcing\Serializer\EventSerializer;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;

use function sprintf;

final class OutputStyle extends SymfonyStyle
{
Expand Down Expand Up @@ -37,4 +40,17 @@ public function message(EventSerializer $serializer, Message $message): void

$this->block($data->payload);
}

public function throwable(Throwable $error): void
{
$number = 1;

do {
$this->error(sprintf('%d) %s', $number, $error->getMessage()));
$this->block($error->getTraceAsString());

$number++;
$error = $error->getPrevious();
} while ($error !== null);
}
}
15 changes: 13 additions & 2 deletions src/Projection/Projection/Projection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace Patchlevel\EventSourcing\Projection\Projection;

use Throwable;

final class Projection
{
public function __construct(
private readonly ProjectionId $id,
private ProjectionStatus $status = ProjectionStatus::New,
private int $position = 0,
private string|null $errorMessage = null,
private Throwable|null $errorObject = null,
) {
}

Expand All @@ -34,6 +37,11 @@ public function errorMessage(): string|null
return $this->errorMessage;
}

public function errorObject(): Throwable|null
{
return $this->errorObject;
}

public function incrementPosition(): void
{
$this->position++;
Expand All @@ -48,6 +56,7 @@ public function booting(): void
{
$this->status = ProjectionStatus::Booting;
$this->errorMessage = null;
$this->errorObject = null;
}

public function isBooting(): bool
Expand All @@ -59,6 +68,7 @@ public function active(): void
{
$this->status = ProjectionStatus::Active;
$this->errorMessage = null;
$this->errorObject = null;
}

public function isActive(): bool
Expand All @@ -76,10 +86,11 @@ public function isOutdated(): bool
return $this->status === ProjectionStatus::Outdated;
}

public function error(string|null $message = null): void
public function error(Throwable|string|null $error = null): void
{
$this->status = ProjectionStatus::Error;
$this->errorMessage = $message;
$this->errorMessage = $error instanceof Throwable ? $error->getMessage() : $error;
$this->errorObject = $error instanceof Throwable ? $error : null;
}

public function isError(): bool
Expand Down
9 changes: 8 additions & 1 deletion src/Projection/Projection/Store/DoctrineStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function all(): ProjectionCollection
->from($this->projectionTable)
->getSQL();

/** @var list<array{name: string, version: int, position: int, status: string, error_message: string|null}> $result */
/** @var list<array{name: string, version: int, position: int, status: string, error_message: string|null, error_object: string|null}> $result */
$result = $this->connection->fetchAllAssociative($sql);

return new ProjectionCollection(
Expand All @@ -68,6 +68,7 @@ static function (array $data) {
ProjectionStatus::from($data['status']),
$data['position'],
$data['error_message'],
ErrorSerializer::unserialize($data['error_object']),
);
},
$result,
Expand All @@ -80,13 +81,16 @@ public function save(Projection ...$projections): void
$this->connection->transactional(
function (Connection $connection) use ($projections): void {
foreach ($projections as $projection) {
$errorObject = ErrorSerializer::serialize($projection->errorObject());

try {
$effectedRows = (int)$connection->update(
$this->projectionTable,
[
'position' => $projection->position(),
'status' => $projection->status()->value,
'error_message' => $projection->errorMessage(),
'error_object' => $errorObject,
],
[
'name' => $projection->id()->name(),
Expand All @@ -106,6 +110,7 @@ function (Connection $connection) use ($projections): void {
'position' => $projection->position(),
'status' => $projection->status()->value,
'error_message' => $projection->errorMessage(),
'error_object' => $errorObject,
],
);
}
Expand Down Expand Up @@ -142,6 +147,8 @@ public function configureSchema(Schema $schema, Connection $connection): void
->setNotnull(true);
$table->addColumn('error_message', Types::STRING)
->setNotnull(false);
$table->addColumn('error_object', Types::BLOB)
->setNotnull(false);

$table->setPrimaryKey(['name', 'version']);
}
Expand Down
37 changes: 37 additions & 0 deletions src/Projection/Projection/Store/ErrorSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Projection\Projection\Store;

use Throwable;

use function serialize;
use function unserialize;

final class ErrorSerializer
{
public static function serialize(Throwable|null $error): string|null
{
if ($error === null) {
return null;
}

return serialize($error);
}

public static function unserialize(string|null $error): Throwable|null
{
if ($error === null) {
return null;
}

$result = @unserialize($error, ['allowed_classes' => true]);

if (!$result instanceof Throwable) {
return null;
}

return $result;
}
}
4 changes: 2 additions & 2 deletions src/Projection/Projectionist/DefaultProjectionist.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function boot(
$e->getMessage(),
));

$projection->error($e->getMessage());
$projection->error($e);
$this->projectionStore->save($projection);

if ($throwByError) {
Expand Down Expand Up @@ -400,7 +400,7 @@ private function handleMessage(Message $message, Projection $projection, bool $t
),
);

$projection->error($e->getMessage());
$projection->error($e);
$this->projectionStore->save($projection);

if ($throwByError) {
Expand Down
7 changes: 6 additions & 1 deletion tests/Unit/Projection/Projection/ProjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Patchlevel\EventSourcing\Projection\Projection\ProjectionId;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionStatus;
use PHPUnit\Framework\TestCase;
use RuntimeException;

/** @covers \Patchlevel\EventSourcing\Projection\Projection\Projection */
final class ProjectionTest extends TestCase
Expand Down Expand Up @@ -65,14 +66,18 @@ public function testError(): void
new ProjectionId('test', 1),
);

$projection->error();
$exception = new RuntimeException('test');

$projection->error($exception);

self::assertEquals(ProjectionStatus::Error, $projection->status());
self::assertFalse($projection->isNew());
self::assertFalse($projection->isBooting());
self::assertFalse($projection->isActive());
self::assertTrue($projection->isError());
self::assertFalse($projection->isOutdated());
self::assertEquals('test', $projection->errorMessage());
self::assertEquals($exception, $projection->errorObject());
}

public function testOutdated(): void
Expand Down
46 changes: 46 additions & 0 deletions tests/Unit/Projection/Projection/Store/ErrorSerializerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Projection\Projection\Store;

use Patchlevel\EventSourcing\Projection\Projection\Store\ErrorSerializer;
use PHPUnit\Framework\TestCase;
use RuntimeException;

use function serialize;

/** @covers \Patchlevel\EventSourcing\Projection\Projection\Store\ErrorSerializer */
final class ErrorSerializerTest extends TestCase
{
public function testSerializeError(): void
{
$error = new RuntimeException('foo');

self::assertEquals(serialize($error), ErrorSerializer::serialize($error));
}

public function testSerializeNull(): void
{
self::assertNull(ErrorSerializer::serialize(null));
}

public function testUnserializeError(): void
{
$error = new RuntimeException('foo');

$serialized = serialize($error);

self::assertEquals($error, ErrorSerializer::unserialize($serialized));
}

public function testUnserializeNull(): void
{
self::assertNull(ErrorSerializer::unserialize(null));
}

public function testUnserializeInvalid(): void
{
self::assertNull(ErrorSerializer::unserialize('foo'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Projection\Projection;
namespace Patchlevel\EventSourcing\Tests\Unit\Projection\Projection\Store;

use Patchlevel\EventSourcing\Projection\Projection\Projection;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionId;
Expand Down
Loading
Loading