Skip to content

Commit

Permalink
use config to convert exception to status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-ht authored and jorge07 committed May 24, 2020
1 parent 789e2cb commit c3cefb4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
8 changes: 8 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ parameters:
env(ELASTIC_HOST): 'elasticsearch:9200'
env(DATABASE_URL): 'mysql://root:api@mysql:3306/api?serverVersion=8.0'

exception_to_status:
InvalidArgumentException: 400
App\Domain\User\Exception\InvalidCredentialsException: 401
App\Domain\User\Exception\ForbiddenException: 403
App\Domain\Shared\Query\Exception\NotFoundException: 404
Broadway\Repository\AggregateNotFoundException: 404

services:
_defaults:
autowire: true
Expand Down Expand Up @@ -85,6 +92,7 @@ services:
App\UI\Http\Rest\EventSubscriber\ExceptionSubscriber:
arguments:
- "%kernel.environment%"
- "%exception_to_status%"
tags:
- { name: 'kernel.event_listener', event: 'kernel.exception' }

Expand Down
36 changes: 14 additions & 22 deletions src/UI/Http/Rest/EventSubscriber/ExceptionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
final class ExceptionSubscriber implements EventSubscriberInterface
{
private string $environment;
private array $exceptionToStatus;

public function __construct(string $environment)
public function __construct(string $environment, array $exceptionToStatus)
{
$this->environment = $environment;
$this->exceptionToStatus = $exceptionToStatus;
}

public static function getSubscribedEvents(): array
Expand Down Expand Up @@ -85,32 +87,22 @@ private function getExceptionMessage(Throwable $exception): string

private function determineStatusCode(Throwable $exception): int
{
// Default status code is always 500
$statusCode = Response::HTTP_INTERNAL_SERVER_ERROR;

switch (true) {
case $exception instanceof HttpExceptionInterface:
$statusCode = $exception->getStatusCode();

break;
case $exception instanceof InvalidCredentialsException:
$statusCode = Response::HTTP_UNAUTHORIZED;

break;
case $exception instanceof ForbiddenException:
$statusCode = Response::HTTP_FORBIDDEN;
$exceptionClass = \get_class($exception);

break;
case $exception instanceof AggregateNotFoundException || $exception instanceof NotFoundException:
$statusCode = Response::HTTP_NOT_FOUND;
foreach ($this->exceptionToStatus as $class => $status) {
if (is_a($exceptionClass, $class, true)) {
return $status;

break;
case $exception instanceof \InvalidArgumentException:
$statusCode = Response::HTTP_BAD_REQUEST;
}
}

break;
// Process HttpExceptionInterface after `exceptionToStatus` to allow overrides from config
if ($exception instanceof HttpExceptionInterface) {
return $exception->getStatusCode();
}

return $statusCode;
// Default status code is always 500
return Response::HTTP_INTERNAL_SERVER_ERROR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function when_json_body_is_invalid(): void
$request->headers->set('Content-Type', 'application/json');

$requestEvent = new RequestEvent(
$this->prophesize(HttpKernelInterface::class)->reveal(),
$this->createMock(HttpKernelInterface::class),
$request,
HttpKernelInterface::MASTER_REQUEST
);
Expand Down

0 comments on commit c3cefb4

Please sign in to comment.