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

III-3173 Fix broken error handler #31

Merged
merged 3 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

use Crell\ApiProblem\ApiProblem;
use CultuurNet\UDB3\HttpFoundation\Response\ApiProblemJsonResponse;
use Exception;

class ErrorHandler
{

public function __invoke(\Exception $exception) : ApiProblem
public function __invoke(Exception $exception): ApiProblemJsonResponse
{
$problem = new ApiProblem($this->formatMessage($exception));
$problem->setStatus($exception->getCode() ?: ApiProblemJsonResponse::HTTP_BAD_REQUEST);
return $problem;
return new ApiProblemJsonResponse($problem);
}

/**
* @param \Exception $exception
* @param Exception $exception
* @return string
*/
private function formatMessage(\Exception $exception): string
private function formatMessage(Exception $exception): string
{
$message = $exception->getMessage();
return preg_replace('/URL CALLED.*/', '', $message);
Expand Down
9 changes: 5 additions & 4 deletions app/ErrorHandlerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace CultuurNet\UDB3\UiTPASService;

use Exception;
use Silex\Application;
use Silex\ServiceProviderInterface;

class ErrorHandlerProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$app->error(
function (Exception $e) {
return (new ErrorHandler())->__invoke($e);
$app['error_handler'] = $app::share(
function () {
return new ErrorHandler();
}
);

$app->error($app['error_handler']);
}

public function boot(Application $app)
Expand Down
35 changes: 24 additions & 11 deletions tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CultuurNet\UDB3\UiTPASService;

use Exception;
use PHPUnit\Framework\TestCase;

class ErrorHandlerTest extends TestCase
Expand All @@ -11,25 +12,33 @@ class ErrorHandlerTest extends TestCase
*/
public function it_creates_api_problem_from_exception()
{
$exception = new \Exception('Exception message', 500);
$exception = new Exception('Exception message', 500);

$errorHandler = new ErrorHandler();
$apiProblem = $errorHandler->__invoke($exception);
$this->assertEquals('Exception message', $apiProblem->getTitle());
$this->assertEquals(500, $apiProblem->getStatus());
$apiProblemResponse = $errorHandler->__invoke($exception);

$this->assertEquals(
'{"title":"Exception message","type":"about:blank","status":500}',
$apiProblemResponse->getContent()
);
$this->assertEquals(500, $apiProblemResponse->getStatusCode());
}

/**
* @test
*/
public function it_creates_api_problem_with_400_status_if_exception_has_no_status_code()
{
$exception = new \Exception('Exception message');
$exception = new Exception('Exception message');

$errorHandler = new ErrorHandler();
$apiProblem = $errorHandler->__invoke($exception);
$this->assertEquals('Exception message', $apiProblem->getTitle());
$this->assertEquals(400, $apiProblem->getStatus());
$apiProblemResponse = $errorHandler->__invoke($exception);

$this->assertEquals(
'{"title":"Exception message","type":"about:blank","status":400}',
$apiProblemResponse->getContent()
);
$this->assertEquals(400, $apiProblemResponse->getStatusCode());
}

/**
Expand All @@ -39,10 +48,14 @@ 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');
$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());
$apiProblemResponse = $errorHandler->__invoke($exception);

$this->assertEquals(
'{"title":"Exception message ","type":"about:blank","status":400}',
$apiProblemResponse->getContent()
);
}
}