8000 [WIP] Better exception page by hason · Pull Request #15792 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WIP] Better exception page #15792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[HttpKernel] Added exception processor for HTTP Exceptions
  • Loading branch information
hason committed Sep 17, 2015
commit 8f4ab6ab605c1955b022e0aa09f67fd8b829033b
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/DebugBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<service id="exception_flattener" class="Symfony\Component\Debug\ExceptionFlattener">
<argument type="collection" />
</service>

<service id="debug.exception_processor.http" class="Symfony\Component\HttpKernel\Debug\HttpFlattenExceptionProcessor" public="false">
<tag name="exception.processor" />
</service>

</services>

</container>
2 changes: 1 addition & 1 deletion src/Symfony/Component/Debug/Exception/FlattenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class FlattenException extends LegacyFlattenException
private $trace;
private $class;
private $statusCode;
private $headers;
private $headers = array();
private $file;
private $line;
private $extras = array();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Debug;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\FlattenExceptionProcessorInterface;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

/**
* @author Martin Hasoň <martin.hason@gmail.com>
*/
class HttpFlattenExceptionProcessor implements FlattenExceptionProcessorInterface
{
/**
* {@inheritdoc}
*/
public function process(\Exception $exception, FlattenException $flattenException, $master)
{
if (!$exception instanceof HttpExceptionInterface) {
$flattenException->setStatusCode($flattenException->getStatusCode() ?: 500);

return;
}

$flattenException->setStatusCode($exception->getStatusCode() ?: 500);
$flattenException->setHeaders(array_merge($flattenException->getHeaders(), $exception->getHeaders()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Tests\Debug;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpKernel\Debug\HttpFlattenExceptionProcessor;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\GoneHttpException;
use Symfony\Component\HttpKernel\Exception\LengthRequiredHttpException;
use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException;
use Symfony\Component\HttpKernel\Exception\PreconditionRequiredHttpException;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;

class HttpFlattenExceptionProcessorTest extends \PHPUnit_Framework_TestCase
{
private $processor;

protected function setUp()
{
$this->processor = new HttpFlattenExceptionProcessor();
}

public function getStatusCode()
{
return array(
array(400, new BadRequestHttpException()),
array(401, new UnauthorizedHttpException('Basic realm="My Realm"')),
array(403, new AccessDeniedHttpException()),
array(404, new NotFoundHttpException()),
array(405, new MethodNotAllowedHttpException(array('POST'))),
array(406, new NotAcceptableHttpException()),
array(409, new ConflictHttpException()),
array(410, new GoneHttpException()),
array(411, new LengthRequiredHttpException()),
array(412, new PreconditionFailedHttpException()),
array(415, new UnsupportedMediaTypeHttpException()),
array(428, new PreconditionRequiredHttpException()),
array(429, new TooManyRequestsHttpException()),
array(500, new \RuntimeException()),
array(503, new ServiceUnavailableHttpException()),
);
}

/**
* @dataProvider getStatusCode
*/
public function testStatusCode($code, $exception)
{
$flattened = new FlattenException();
$this->processor->process($exception, $flattened, true);
$this->assertEquals($code, $flattened->getStatusCode());
}

public function testCustomStatusCode()
{
$flattened = new FlattenException();
$flattened->setStatusCode(403);
$this->processor->process(new \RuntimeException(), $flattened, true);
$this->assertEquals(403, $flattened->getStatusCode());
}

public function getHeaders()
{
return array(
array(array('Allow' => 'POST'), new MethodNotAllowedHttpException(array('POST'))),
array(array('WWW-Authenticate' => 'Basic realm="My Realm"'), new UnauthorizedHttpException('Basic realm="My Realm"')),
array(array('Retry-After' => 'Fri, 31 Dec 1999 23:59:59 GMT'), new ServiceUnavailableHttpException('Fri, 31 Dec 1999 23:59:59 GMT')),
array(array('Retry-After' => 120), new ServiceUnavailableHttpException(120)),
array(array('Retry-After' => 'Fri, 31 Dec 1999 23:59:59 GMT'), new TooManyRequestsHttpException('Fri, 31 Dec 1999 23:59:59 GMT')),
array(array('Retry-After' => 120), new TooManyRequestsHttpException(120)),
);
}

/**
* @dataProvider getHeaders
*/
public function testHeadersForHttpException($headers, $exception)
{
$flattened = new FlattenException();
$this->processor->process($exception, $flattened, true);
$this->assertEquals($headers, $flattened->getHeaders());
}
}
0