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()); + } +}