-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #585 from patchlevel/throw-by-error-subscription-e…
…ngine add throw on error subscription engine decorator
- Loading branch information
Showing
5 changed files
with
422 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Subscription\Engine; | ||
|
||
use RuntimeException; | ||
|
||
use function array_map; | ||
use function implode; | ||
use function sprintf; | ||
|
||
final class ErrorDetected extends RuntimeException | ||
{ | ||
/** @param list<Error> $errors */ | ||
public function __construct( | ||
public readonly array $errors, | ||
) { | ||
$sentences = array_map( | ||
static fn (Error $error) => sprintf( | ||
'Subscription %s: %s', | ||
$error->subscriptionId, | ||
$error->message, | ||
), | ||
$errors, | ||
); | ||
|
||
parent::__construct("Error in subscription engine detected.\n" . implode("\n", $sentences)); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/Subscription/Engine/ThrowOnErrorSubscriptionEngine.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Subscription\Engine; | ||
|
||
use Patchlevel\EventSourcing\Subscription\Subscription; | ||
|
||
final class ThrowOnErrorSubscriptionEngine implements SubscriptionEngine | ||
{ | ||
public function __construct( | ||
private readonly SubscriptionEngine $parent, | ||
) { | ||
} | ||
|
||
public function setup(SubscriptionEngineCriteria|null $criteria = null, bool $skipBooting = false): Result | ||
{ | ||
return $this->throwOnError($this->parent->setup($criteria, $skipBooting)); | ||
} | ||
|
||
public function boot(SubscriptionEngineCriteria|null $criteria = null, int|null $limit = null): ProcessedResult | ||
{ | ||
return $this->throwOnError($this->parent->boot($criteria, $limit)); | ||
} | ||
|
||
public function run(SubscriptionEngineCriteria|null $criteria = null, int|null $limit = null): ProcessedResult | ||
{ | ||
return $this->throwOnError($this->parent->run($criteria, $limit)); | ||
} | ||
|
||
public function teardown(SubscriptionEngineCriteria|null $criteria = null): Result | ||
{ | ||
return $this->throwOnError($this->parent->teardown($criteria)); | ||
} | ||
|
||
public function remove(SubscriptionEngineCriteria|null $criteria = null): Result | ||
{ | ||
return $this->throwOnError($this->parent->remove($criteria)); | ||
} | ||
|
||
public function reactivate(SubscriptionEngineCriteria|null $criteria = null): Result | ||
{ | ||
return $this->throwOnError($this->parent->reactivate($criteria)); | ||
} | ||
|
||
public function pause(SubscriptionEngineCriteria|null $criteria = null): Result | ||
{ | ||
return $this->throwOnError($this->parent->pause($criteria)); | ||
} | ||
|
||
/** @return list<Subscription> */ | ||
public function subscriptions(SubscriptionEngineCriteria|null $criteria = null): array | ||
{ | ||
return $this->parent->subscriptions($criteria); | ||
} | ||
|
||
/** | ||
* @param T $result | ||
* | ||
* @return T | ||
* | ||
* @template T of Result|ProcessedResult | ||
*/ | ||
private function throwOnError(Result|ProcessedResult $result): Result|ProcessedResult | ||
{ | ||
$errors = $result->errors; | ||
|
||
if ($errors !== []) { | ||
throw new ErrorDetected($errors); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Tests\Unit\Subscription\Engine; | ||
|
||
use Patchlevel\EventSourcing\Subscription\Engine\Error; | ||
use Patchlevel\EventSourcing\Subscription\Engine\ErrorDetected; | ||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
|
||
/** @covers \Patchlevel\EventSourcing\Subscription\Engine\ErrorDetected */ | ||
final class ErrorDetectedTest extends TestCase | ||
{ | ||
public function testError(): void | ||
{ | ||
$errors = [ | ||
new Error('id1', 'error1', new RuntimeException('error1')), | ||
new Error('id2', 'error2', new RuntimeException('error2')), | ||
]; | ||
|
||
$errorDetected = new ErrorDetected($errors); | ||
|
||
self::assertSame($errors, $errorDetected->errors); | ||
self::assertSame( | ||
"Error in subscription engine detected.\nSubscription id1: error1\nSubscription id2: error2", | ||
$errorDetected->getMessage(), | ||
); | ||
} | ||
} |
Oops, something went wrong.