Skip to content

Commit

Permalink
Using the response factory for the validation handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Jan 6, 2025
1 parent 10b4520 commit d9c266f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
21 changes: 10 additions & 11 deletions src/HttpExceptionToProblemDetailsKernelListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Phauthentic\Symfony\ProblemDetails;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Throwable;
Expand All @@ -26,6 +26,11 @@
*/
class HttpExceptionToProblemDetailsKernelListener
{
public function __construct(
protected ProblemDetailsFactoryInterface $problemDetailsFactory
) {
}

public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
Expand All @@ -41,18 +46,12 @@ private function isAHttpException(Throwable $exception): bool
return $exception instanceof HttpException;
}

private function buildResponse(HttpException $httpException): JsonResponse
private function buildResponse(HttpException $httpException): Response
{
return new JsonResponse(
data: [
'type' => 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/' . $httpException->getStatusCode(),
'title' => $httpException->getMessage(),
'status' => $httpException->getStatusCode(),
],
return $this->problemDetailsFactory->createResponse(
status: $httpException->getStatusCode(),
headers: [
'Content-Type' => 'application/problem+json',
]
type: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/' . $httpException->getStatusCode(),
title: $httpException->getMessage()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Phauthentic\Symfony\ProblemDetails\Tests\Unit;

use Exception;
use Phauthentic\Symfony\ProblemDetails\ProblemDetailsFactory;
use PHPUnit\Framework\TestCase;
use Phauthentic\Symfony\ProblemDetails\HttpExceptionToProblemDetailsKernelListener;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand All @@ -27,7 +28,9 @@ public function testOnKernelExceptionWithHttpException(): void
server: ['HTTP_ACCEPT' => 'application/json']
);
$event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception);
$listener = new HttpExceptionToProblemDetailsKernelListener();
$listener = new HttpExceptionToProblemDetailsKernelListener(
new ProblemDetailsFactory()
);

// Act
$listener->onKernelException($event);
Expand Down Expand Up @@ -55,7 +58,9 @@ public function testOnKernelExceptionWithNonHttpException(): void
server: ['HTTP_ACCEPT' => 'application/json']
);
$event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception);
$listener = new HttpExceptionToProblemDetailsKernelListener();
$listener = new HttpExceptionToProblemDetailsKernelListener(
new ProblemDetailsFactory()
);

// Act
$listener->onKernelException($event);
Expand Down

0 comments on commit d9c266f

Please sign in to comment.