diff --git a/app/ErrorHandler.php b/app/ErrorHandler.php index 5c7c776..0812d71 100644 --- a/app/ErrorHandler.php +++ b/app/ErrorHandler.php @@ -4,22 +4,22 @@ use Crell\ApiProblem\ApiProblem; use CultuurNet\UDB3\HttpFoundation\Response\ApiProblemJsonResponse; +use Exception; class ErrorHandler { - - public function __invoke(\Exception $exception) : ApiProblem + public function __invoke(Exception $exception): ApiProblemJsonResponse { $problem = new ApiProblem($this->formatMessage($exception)); $problem->setStatus($exception->getCode() ?: ApiProblemJsonResponse::HTTP_BAD_REQUEST); - return $problem; + return new ApiProblemJsonResponse($problem); } /** - * @param \Exception $exception + * @param Exception $exception * @return string */ - private function formatMessage(\Exception $exception): string + private function formatMessage(Exception $exception): string { $message = $exception->getMessage(); return preg_replace('/URL CALLED.*/', '', $message); diff --git a/app/ErrorHandlerProvider.php b/app/ErrorHandlerProvider.php index 6c2f99a..2c067e0 100644 --- a/app/ErrorHandlerProvider.php +++ b/app/ErrorHandlerProvider.php @@ -2,7 +2,6 @@ namespace CultuurNet\UDB3\UiTPASService; -use Exception; use Silex\Application; use Silex\ServiceProviderInterface; @@ -10,11 +9,13 @@ class ErrorHandlerProvider implements ServiceProviderInterface { public function register(Application $app) { - $app->error( - function (Exception $e) { - return (new ErrorHandler())->__invoke($e); + $app['error_handler'] = $app::share( + function () { + return new ErrorHandler(); } ); + + $app->error($app['error_handler']); } public function boot(Application $app) diff --git a/tests/ErrorHandlerTest.php b/tests/ErrorHandlerTest.php index 492b1ca..730950e 100644 --- a/tests/ErrorHandlerTest.php +++ b/tests/ErrorHandlerTest.php @@ -2,6 +2,7 @@ namespace CultuurNet\UDB3\UiTPASService; +use Exception; use PHPUnit\Framework\TestCase; class ErrorHandlerTest extends TestCase @@ -11,12 +12,16 @@ class ErrorHandlerTest extends TestCase */ public function it_creates_api_problem_from_exception() { - $exception = new \Exception('Exception message', 500); + $exception = new Exception('Exception message', 500); $errorHandler = new ErrorHandler(); - $apiProblem = $errorHandler->__invoke($exception); - $this->assertEquals('Exception message', $apiProblem->getTitle()); - $this->assertEquals(500, $apiProblem->getStatus()); + $apiProblemResponse = $errorHandler->__invoke($exception); + + $this->assertEquals( + '{"title":"Exception message","type":"about:blank","status":500}', + $apiProblemResponse->getContent() + ); + $this->assertEquals(500, $apiProblemResponse->getStatusCode()); } /** @@ -24,12 +29,16 @@ public function it_creates_api_problem_from_exception() */ public function it_creates_api_problem_with_400_status_if_exception_has_no_status_code() { - $exception = new \Exception('Exception message'); + $exception = new Exception('Exception message'); $errorHandler = new ErrorHandler(); - $apiProblem = $errorHandler->__invoke($exception); - $this->assertEquals('Exception message', $apiProblem->getTitle()); - $this->assertEquals(400, $apiProblem->getStatus()); + $apiProblemResponse = $errorHandler->__invoke($exception); + + $this->assertEquals( + '{"title":"Exception message","type":"about:blank","status":400}', + $apiProblemResponse->getContent() + ); + $this->assertEquals(400, $apiProblemResponse->getStatusCode()); } /** @@ -39,10 +48,14 @@ public function it_strips_URL_CALLED_from_api_problem_message_title() { // the resulting ApiProblem title should be stripped // of everything after URL CALLED - $exception = new \Exception('Exception message URL CALLED: https://acc.uitid.be/uitid/rest/uitpas/cultureevent/de343e38-d656-4928-96bc-55578e0d94ec/cardsystems POST DATA: cardSystemId=3'); + $exception = new Exception('Exception message URL CALLED: https://acc.uitid.be/uitid/rest/uitpas/cultureevent/de343e38-d656-4928-96bc-55578e0d94ec/cardsystems POST DATA: cardSystemId=3'); $errorHandler = new ErrorHandler(); - $apiProblem = $errorHandler->__invoke($exception); - $this->assertEquals('Exception message ', $apiProblem->getTitle()); + $apiProblemResponse = $errorHandler->__invoke($exception); + + $this->assertEquals( + '{"title":"Exception message ","type":"about:blank","status":400}', + $apiProblemResponse->getContent() + ); } }