This repository has been archived by the owner on Jun 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add logic to strip UiTPAS API request information from Error Response
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace CultuurNet\UDB3\UiTPASService; | ||
|
||
use Crell\ApiProblem\ApiProblem; | ||
use CultuurNet\UDB3\HttpFoundation\Response\ApiProblemJsonResponse; | ||
|
||
class ErrorHandler | ||
{ | ||
|
||
public function __invoke(\Exception $exception) : ApiProblem | ||
{ | ||
$problem = new ApiProblem($this->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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace CultuurNet\UDB3\UiTPASService; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class ErrorHandlerTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function it_creates_api_problem_from_exception() | ||
{ | ||
$exception = new \Exception('Exception message', 500); | ||
|
||
$errorHandler = new ErrorHandler(); | ||
$apiProblem = $errorHandler->__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()); | ||
} | ||
} |