From d2b6bec971cca0b5b1a0d9759a42be48393e7cb6 Mon Sep 17 00:00:00 2001 From: zvonimir Date: Thu, 12 Dec 2019 12:16:50 +0100 Subject: [PATCH] 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()); + } +}