From d2b6bec971cca0b5b1a0d9759a42be48393e7cb6 Mon Sep 17 00:00:00 2001 From: zvonimir Date: Thu, 12 Dec 2019 12:16:50 +0100 Subject: [PATCH 1/3] Implement ErroHandler - add logic to strip UiTPAS API request information from Error Response --- app/ErrorHandler.php | 28 ++++++++++++++++++++++ tests/ErrorHandlerTest.php | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 app/ErrorHandler.php create mode 100644 tests/ErrorHandlerTest.php diff --git a/app/ErrorHandler.php b/app/ErrorHandler.php new file mode 100644 index 0000000..447fe1d --- /dev/null +++ b/app/ErrorHandler.php @@ -0,0 +1,28 @@ +formatMessage($exception)); + $problem->setStatus($exception->getCode() ?: ApiProblemJsonResponse::HTTP_BAD_REQUEST); + return $problem; + } + + /** + * @param \Exception $exception + * @return string + */ + private function formatMessage(\Exception $exception): string + { + $message = $exception->getMessage(); + return preg_replace('/URL CALLED.*/','',$message); + } + +} diff --git a/tests/ErrorHandlerTest.php b/tests/ErrorHandlerTest.php new file mode 100644 index 0000000..492b1ca --- /dev/null +++ b/tests/ErrorHandlerTest.php @@ -0,0 +1,48 @@ +__invoke($exception); + $this->assertEquals('Exception message', $apiProblem->getTitle()); + $this->assertEquals(500, $apiProblem->getStatus()); + } + + /** + * @test + */ + public function it_creates_api_problem_with_400_status_if_exception_has_no_status_code() + { + $exception = new \Exception('Exception message'); + + $errorHandler = new ErrorHandler(); + $apiProblem = $errorHandler->__invoke($exception); + $this->assertEquals('Exception message', $apiProblem->getTitle()); + $this->assertEquals(400, $apiProblem->getStatus()); + } + + /** + * @test + */ + 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'); + + $errorHandler = new ErrorHandler(); + $apiProblem = $errorHandler->__invoke($exception); + $this->assertEquals('Exception message ', $apiProblem->getTitle()); + } +} From 555d30ae4ac3a2fc79b40e59e71abfe7243a5acb Mon Sep 17 00:00:00 2001 From: zvonimir Date: Thu, 12 Dec 2019 12:19:33 +0100 Subject: [PATCH 2/3] Use ErrorHandler in ErrorHandlerProvider --- app/ErrorHandlerProvider.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/app/ErrorHandlerProvider.php b/app/ErrorHandlerProvider.php index 2191e64..6c2f99a 100644 --- a/app/ErrorHandlerProvider.php +++ b/app/ErrorHandlerProvider.php @@ -2,8 +2,6 @@ namespace CultuurNet\UDB3\UiTPASService; -use Crell\ApiProblem\ApiProblem; -use CultuurNet\UDB3\HttpFoundation\Response\ApiProblemJsonResponse; use Exception; use Silex\Application; use Silex\ServiceProviderInterface; @@ -14,19 +12,11 @@ public function register(Application $app) { $app->error( function (Exception $e) { - $problem = $this->createNewApiProblem($e); - return new ApiProblemJsonResponse($problem); + return (new ErrorHandler())->__invoke($e); } ); } - protected function createNewApiProblem(Exception $e): ApiProblem - { - $problem = new ApiProblem($e->getMessage()); - $problem->setStatus($e->getCode() ?: ApiProblemJsonResponse::HTTP_BAD_REQUEST); - return $problem; - } - public function boot(Application $app) { } From 443bb97da7dd9d738e37bcc565679e200efc4d8e Mon Sep 17 00:00:00 2001 From: zvonimir Date: Thu, 12 Dec 2019 14:54:08 +0100 Subject: [PATCH 3/3] Improve code formatting --- app/ErrorHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/ErrorHandler.php b/app/ErrorHandler.php index 447fe1d..5c7c776 100644 --- a/app/ErrorHandler.php +++ b/app/ErrorHandler.php @@ -22,7 +22,7 @@ public function __invoke(\Exception $exception) : ApiProblem private function formatMessage(\Exception $exception): string { $message = $exception->getMessage(); - return preg_replace('/URL CALLED.*/','',$message); + return preg_replace('/URL CALLED.*/', '', $message); } }