Skip to content

Commit fede9c0

Browse files
committed
Improving exception handling to make it easier and isolate exception concerns from everywhere else. Plus, definining the first draft of the DDD architecture, yay!
1 parent 8dca99d commit fede9c0

File tree

12 files changed

+42
-51
lines changed

12 files changed

+42
-51
lines changed

config/services.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ services:
1515
# this creates a service per class whose id is the fully-qualified class name
1616
App\:
1717
resource: '../src/*'
18-
exclude: '../src/{Entity,Migrations,Tests}'
1918

2019
# controllers are imported separately to make sure services can be injected
2120
# as action arguments even if you don't extend any base controller class
@@ -25,7 +24,7 @@ services:
2524

2625
# add more service definitions when explicit configuration is needed
2726
# please note that last definitions always *replace* previous ones
28-
App\EventListener\ExceptionListener:
27+
App\Infrastructure\EventListener\Symfony\ExceptionListener:
2928
arguments: ['%env(APP_ENV)%']
3029
tags:
3130
- { name: kernel.event_listener, event: kernel.exception }
File renamed without changes.
File renamed without changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Domain\Exception;
4+
5+
/**
6+
* Global exception within our system, which will ALWAYS have a well-defined error code
7+
* Such error code is defined in one of the files under ErrorCode/, which will be in this folder
8+
*/
9+
class AppException extends \Exception
10+
{
11+
/**
12+
* @var string
13+
*/
14+
private $internalErrorCode;
15+
16+
public function __construct(string $internalErrorCode, string $message = '')
17+
{
18+
$this->internalErrorCode = $internalErrorCode;
19+
parent::__construct($message);
20+
}
21+
22+
public function internalErrorCode(): string
23+
{
24+
return $this->internalErrorCode;
25+
}
26+
}

src/Exception/ErrorCode/GlobalErrorCodes.php renamed to src/Domain/Exception/ErrorCode/GlobalErrorCodes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Exception\ErrorCode;
3+
namespace App\Domain\Exception\ErrorCode;
44

55
final class GlobalErrorCodes
66
{

src/Domain/Model/.gitignore

Whitespace-only changes.

src/Domain/Repository/.gitignore

Whitespace-only changes.

src/Exception/AppException.php

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

src/EventListener/ExceptionListener.php renamed to src/Infrastructure/EventListener/Symfony/ExceptionListener.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace App\EventListener;
3+
namespace App\Infrastructure\EventListener\Symfony;
44

5-
use App\Exception\AppException;
6-
use App\Exception\ErrorCode\GlobalErrorCodes;
5+
use App\Domain\Exception\AppException;
6+
use App\Domain\Exception\ErrorCode\GlobalErrorCodes;
77
use Symfony\Component\HttpFoundation\JsonResponse;
88
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
99
use Symfony\Component\HttpFoundation\Response;
@@ -30,7 +30,7 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
3030
$response = new JsonResponse();
3131

3232
if ($exception instanceof AppException) {
33-
$response->setStatusCode($exception->getHttpErrorCode());
33+
$response->setStatusCode($this->buildHttpErrorCodeFromInternalErrorCode($exception->internalErrorCode()));
3434
} elseif ($exception instanceof HttpExceptionInterface) {
3535
$response->setStatusCode($exception->getStatusCode());
3636
} else {
@@ -51,7 +51,7 @@ private function buildErrorMessage(\Exception $exception): array
5151
{
5252
$errorMessage = [];
5353
if ($exception instanceof AppException) {
54-
$errorMessage['errorCode'] = $exception->getInternalErrorCode();
54+
$errorMessage['errorCode'] = $exception->internalErrorCode();
5555
} else {
5656
$errorMessage['errorCode'] = GlobalErrorCodes::INTERNAL_SERVER_DRAMA;
5757
}
@@ -74,4 +74,13 @@ private function isProductionEnvironment(): bool
7474
{
7575
return in_array($this->environment, ['pre', 'pro']);
7676
}
77+
78+
private function buildHttpErrorCodeFromInternalErrorCode(string $internalErrorCode): string
79+
{
80+
switch ($internalErrorCode) {
81+
case GlobalErrorCodes::INTERNAL_SERVER_DRAMA:
82+
default:
83+
return Response::HTTP_INTERNAL_SERVER_ERROR;
84+
}
85+
}
7786
}

src/Infrastructure/Query/.gitignore

Whitespace-only changes.

0 commit comments

Comments
 (0)