Skip to content

Commit

Permalink
[HttpKernel] Reworking the HttpException class constructor to be more…
Browse files Browse the repository at this point in the history
… consistent with normal OO classes. Additionally, the base HttpException constructor was changed to require a code argument as it doesn't make sense to create an exception that will translate into a status code of 0 (in fact it'll cause a strange error).
  • Loading branch information
weaverryan authored and fabpot committed Dec 10, 2010
1 parent ab7ad48 commit be94dab
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
Expand Up @@ -18,7 +18,7 @@
*/
class ForbiddenHttpException extends HttpException
{
public function __construct($message = '', $code = 0, \Exception $previous = null)
public function __construct($message = '', \Exception $previous = null)
{
if (!$message) {
$message = 'Forbidden';
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/HttpKernel/Exception/HttpException.php
Expand Up @@ -20,4 +20,13 @@
*/
class HttpException extends \Exception
{
/**
* Constructor overridden to require the code, which is the status code.
*
* @see \Exception
*/
public function __construct($message, $code, \Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
Expand Up @@ -18,7 +18,7 @@
*/
class NotFoundHttpException extends HttpException
{
public function __construct($message = '', $code = 0, \Exception $previous = null)
public function __construct($message = '', \Exception $previous = null)
{
if (!$message) {
$message = 'Not Found';
Expand Down
Expand Up @@ -18,7 +18,7 @@
*/
class UnauthorizedHttpException extends HttpException
{
public function __construct($message = '', $code = 0, \Exception $previous = null)
public function __construct($message = '', \Exception $previous = null)
{
if (!$message) {
$message = 'Unauthorized';
Expand Down
@@ -0,0 +1,45 @@
<?php

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

namespace Symfony\Tests\Component\HttpKernel\Exception;
use Symfony\Component\HttpKernel\Exception\FlattenException;

class FlattenExceptionTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider flattenDataProvider
*/
public function testFlattenHttpException(\Exception $exception, $statusCode)
{
$flattened = FlattenException::create($exception);

$this->assertEquals($statusCode, $flattened->getStatusCode(), 'A HttpKernel exception uses the error code as the status code.');
$this->assertEquals($exception->getMessage(), $flattened->getMessage(), 'The message is copied from the original exception.');
$this->assertEquals($exception->getCode(), $flattened->getCode(), 'The code is copied from the original exception.');
$this->assertEquals(get_class($exception), $flattened->getClass(), 'The class is set to the class of the original exception');

}

public function flattenDataProvider()
{
return array(
array(new TestHttpException('test', 404), 404),
array(new \Exception('test', 123), 500),
);
}
}

use Symfony\Component\HttpKernel\Exception\HttpException;

// stub Exception class that extends HttpException
class TestHttpException extends HttpException
{
}

0 comments on commit be94dab

Please sign in to comment.