Skip to content

Commit 2acbba4

Browse files
authored
Move ApplicationException (#299)
Move ApplicationException to Domain and rename it to DomainException. This avoids casting domain exceptions to application level exceptions. The DomainException is meant to be used as a Layer Supertype.
1 parent 66fd165 commit 2acbba4

18 files changed

+269
-268
lines changed

config/config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ services:
101101
arguments: ['@logger']
102102
tags: [{ name: kernel.event_listener, method: 'messageFailed' }]
103103

104-
Gaming\Common\Bus\Integration\ReplyWithErrorResponseOnMessageFailedListener:
104+
Gaming\Common\Domain\Integration\ReplyWithErrorResponseOnMessageFailedListener:
105105
tags: [{ name: kernel.event_listener, method: 'messageFailed', priority: 50 }]
106106

107107
Gaming\Common\EventStore\Integration\Symfony\ResetServicesListener:
@@ -139,7 +139,7 @@ services:
139139
arguments: ['%env(APP_METRICS_SOCKET)%', '@gaming.prometheus.registry', 'app', '@logger']
140140

141141
gaming.form-violation-mapper:
142-
class: Gaming\Common\Bus\Integration\FormViolationMapper
142+
class: Gaming\Common\Domain\Integration\FormViolationMapper
143143

144144
gaming.scheduler:
145145
class: Gaming\Common\Scheduler\PcntlScheduler
@@ -171,8 +171,8 @@ services:
171171
tags: [{ name: kernel.event_listener, event: kernel.exception, priority: -100 }]
172172

173173
# Lower priority so that the profiler is respected.
174-
Gaming\Common\Bus\Integration\ApplicationExceptionToJsonListener:
175-
arguments: [[]]
174+
Gaming\Common\Domain\Integration\RespondWithViolationsAsJsonOnKernelExceptionListener:
175+
arguments: [[], '@translator']
176176
tags: [{ name: kernel.event_listener, event: kernel.exception, priority: -99 }]
177177

178178
Gaming\WebInterface\Infrastructure\Symfony\HandleFragmentExceptions:

src/Common/Bus/Bus.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Gaming\Common\Bus;
66

77
use Exception;
8-
use Gaming\Common\Bus\Exception\ApplicationException;
98
use Gaming\Common\Bus\Exception\BusException;
109

1110
interface Bus
@@ -16,7 +15,6 @@ interface Bus
1615
* @param Request<TResponse> $request
1716
*
1817
* @return TResponse
19-
* @throws ApplicationException
2018
* @throws BusException
2119
* @throws Exception Any application based exception
2220
*/

src/Common/Bus/Exception/ApplicationException.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Common/Bus/Integration/ApplicationExceptionToJsonListener.php

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/Common/Bus/Integration/SymfonyValidatorBus.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
namespace Gaming\Common\Bus\Integration;
66

77
use Gaming\Common\Bus\Bus;
8-
use Gaming\Common\Bus\Exception\ApplicationException;
98
use Gaming\Common\Bus\Request;
10-
use Gaming\Common\Bus\Violation;
11-
use Gaming\Common\Bus\ViolationParameter;
9+
use Gaming\Common\Domain\Exception\DomainException;
10+
use Gaming\Common\Domain\Exception\Violation;
11+
use Gaming\Common\Domain\Exception\ViolationParameter;
12+
use Gaming\Common\Domain\Exception\Violations;
1213
use Symfony\Component\Validator\ConstraintViolationInterface;
1314
use Symfony\Component\Validator\ConstraintViolationListInterface;
1415
use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -26,8 +27,8 @@ public function handle(Request $request): mixed
2627
$symfonyViolations = $this->validator->validate($request);
2728

2829
if ($symfonyViolations->count() > 0) {
29-
throw new ApplicationException(
30-
$this->mapFromSymfonyViolations($symfonyViolations)
30+
throw new DomainException(
31+
new Violations(...$this->mapFromSymfonyViolations($symfonyViolations))
3132
);
3233
}
3334

@@ -43,9 +44,9 @@ private function mapFromSymfonyViolations(ConstraintViolationListInterface $symf
4344
{
4445
return array_map(
4546
fn(ConstraintViolationInterface $constraintViolation): Violation => new Violation(
46-
$constraintViolation->getPropertyPath(),
4747
(string)$constraintViolation->getMessage(),
48-
$this->mapFromSymfonyParameters($constraintViolation->getParameters())
48+
$this->mapFromSymfonyParameters($constraintViolation->getParameters()),
49+
$constraintViolation->getPropertyPath()
4950
),
5051
iterator_to_array($symfonyViolations)
5152
);

src/Common/Bus/Violation.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/Common/Bus/ViolationParameter.php

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gaming\Common\Domain\Exception;
6+
7+
use Exception;
8+
9+
/**
10+
* Represents an exception raised when a domain invariant is violated.
11+
* It's meant to be used as a Layer Supertype that encapsulates violations
12+
* that can be translated into user-friendly error messages.
13+
*/
14+
class DomainException extends Exception
15+
{
16+
public function __construct(
17+
public readonly Violations $violations
18+
) {
19+
parent::__construct('Domain exception occurred with ' . $violations->count() . ' violation(s).');
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gaming\Common\Domain\Exception;
6+
7+
final class Violation
8+
{
9+
/**
10+
* @param ViolationParameter[] $parameters
11+
*/
12+
public function __construct(
13+
public readonly string $identifier,
14+
public readonly array $parameters = [],
15+
public readonly string $propertyPath = ''
16+
) {
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gaming\Common\Domain\Exception;
6+
7+
final class ViolationParameter
8+
{
9+
public function __construct(
10+
public readonly string $name,
11+
public readonly bool|int|float|string $value
12+
) {
13+
}
14+
}

0 commit comments

Comments
 (0)