[HttpKernel] moved some mis-placed logic to FlattenException

This commit is contained in:
Fabien Potencier 2012-07-13 09:48:10 +02:00
parent fdf320d642
commit bd18907150
3 changed files with 14 additions and 8 deletions

View File

@ -62,15 +62,9 @@ class ExceptionListener implements EventSubscriberInterface
$logger = $this->logger instanceof DebugLoggerInterface ? $this->logger : null;
$flattenException = FlattenException::create($exception);
if ($exception instanceof HttpExceptionInterface) {
$flattenException->setStatusCode($exception->getStatusCode());
$flattenException->setHeaders($exception->getHeaders());
}
$attributes = array(
'_controller' => $this->controller,
'exception' => $flattenException,
'exception' => FlattenException::create($exception),
'logger' => $logger,
'format' => $request->getRequestFormat(),
);

View File

@ -36,8 +36,13 @@ class FlattenException
$e->setMessage($exception->getMessage());
$e->setCode($exception->getCode());
if ($exception instanceof HttpExceptionInterface) {
$statusCode = $exception->getStatusCode();
$headers = array_merge($headers, $exception->getHeaders());
}
if (null === $statusCode) {
$statusCode = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
$statusCode = 500;
}
$e->setStatusCode($statusCode);

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\HttpKernel\Tests\Exception;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
class FlattenExceptionTest extends \PHPUnit_Framework_TestCase
{
@ -28,6 +29,12 @@ class FlattenExceptionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('404', $flattened->getStatusCode());
}
public function testHeadersForHttpException()
{
$flattened = FlattenException::create(new MethodNotAllowedHttpException(array('POST')));
$this->assertEquals(array('Allow' => 'POST'), $flattened->getHeaders());
}
/**
* @dataProvider flattenDataProvider
*/