Skip to content
This repository has been archived by the owner on Jun 23, 2021. It is now read-only.

Commit

Permalink
Merge 555d30a into 5b16929
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad126 committed Dec 12, 2019
2 parents 5b16929 + 555d30a commit 99036e4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
28 changes: 28 additions & 0 deletions app/ErrorHandler.php
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);
}

}
12 changes: 1 addition & 11 deletions app/ErrorHandlerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
}
Expand Down
48 changes: 48 additions & 0 deletions tests/ErrorHandlerTest.php
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());
}
}

0 comments on commit 99036e4

Please sign in to comment.