8000 merged branch ramsey/feature/additional-http-exceptions (PR #5312) · glsee/symfony@27ee846 · GitHub
[go: up one dir, main page]

Skip to content

Commit 27ee846

Browse files
committed
merged branch ramsey/feature/additional-http-exceptions (PR symfony#5312)
This PR was squashed before being merged into the master branch (closes symfony#5312). Commits ------- e0c4d99 [HttpKernel] Additional HTTP exceptions Discussion ---------- [HttpKernel] Additional HTTP exceptions I wanted to continue using exceptions for many other types of HTTP 4xx and 5xx status codes, particularly because I like the notion that I can trap the exception based on the type, rather than inspecting HttpException to see what status code it contains. I've decided to contribute these back to the Symfony HttpKernel component, in case others find them useful. * Bug fix: no * Feature addition: yes * Backwards compatibility break: no * Symfony2 tests pass: yes * Fixes the following tickets: - * Todo: - * License of the code: MIT * Documentation PR: n/a This pull request provides the following new HttpKernel Exceptions: Exception | Applicable HTTP Status Code ---------------------------------- | --------------------------------------- BadRequestHttpException | `400 Bad Request` ConflictHttpException | `409 Conflict` GoneHttpException | `410 Gone` InternalServerErrorHttpException | `500 Internal Server Error` LengthRequiredHttpException | `411 Length Required` NotAcceptableHttpException | `406 Not Acceptable` PreconditionFailedHttpException | `412 Precondition Failed` PreconditionRequiredHttpException | `428 Precondition Required`† ServiceUnavailableHttpException | `503 Service Unavailable` TooManyRequestsHttpException | `429 Too Many Requests`† UnauthorizedHttpException | `401 Unauthorized` UnsupportedMediaTypeHttpException | `415 Unsupported Media Type` All the tests have been placed in the FlattenExceptionTest, since that's where the previous status code and method tests for HttpException exceptions are located, but I can move them to a more logical location, if needed. † These codes have been included from [RFC 6585, Additional HTTP Status Codes](http://tools.ietf.org/html/rfc6585). --------------------------------------------------------------------------- by stof at 2012-08-21T23:10:45Z I would remove the InternalServerError one, as you get a 500 for any non-http exception anyway --------------------------------------------------------------------------- by stof at 2012-08-21T23:11:34Z and please rebase your branch to get rid of the merge commit at the beginning of your PR --------------------------------------------------------------------------- by ramsey at 2012-10-03T20:22:59Z @stof Can you provide some pointers on how to get rid of the merge commit through a rebase? I generally avoid rebase, so doing this is fairly new to me. I realize that what created the commit was a non-fast-forward merge, so I just need to understand how to get it to apply merges cleanly without creating merge commits like that. Thanks! --------------------------------------------------------------------------- by alexandresalome at 2012-10-04T07:14:00Z @ramsey # suppose you are on branch-feature # given origin is the symfony repository git pull --rebase origin/master # equivalent to git fetch origin git rebase origin/master If you want to rebase your branch on 2.1: git pull --rebase origin/2.1 --------------------------------------------------------------------------- by stof at 2012-10-04T18:52:22Z @ramsey http://symfony.com/doc/current/contributing/code/patches.html#rework-your-patch for the documentation about the way to do it --------------------------------------------------------------------------- by stof at 2012-10-13T20:44:26Z @ramsey ping --------------------------------------------------------------------------- by fabpot at 2012-10-29T09:47:36Z I'm going to finish this PR, but symfony#5862 should be merged first. --------------------------------------------------------------------------- by ramsey at 2012-10-29T15:36:31Z Sorry I've been sitting on this for so long. I haven't had time to figure out how to get rid of the merge commit, as @stof requested. That's the only outstanding issue here, as far as I know. --------------------------------------------------------------------------- by fabpot at 2012-10-29T15:47:54Z I can see another issue. For some HTTP codes, you can/should/must add some headers or some content (like for the 406 status code). Don't worry about the merge commit or the rebasing, I will take care of that myself when merging the PR. --------------------------------------------------------------------------- by ramsey at 2012-10-29T16:05:04Z I don't know what header is required for the 406 status code. RFC 2616 doesn't specify one. It does say "the response should include an entity containing a list of available entity characteristics and locations." In other exceptions, such as UnauthorizedHttpException, TooManyRequestsHttpException, and ServiceUnavailableException, I did provide the required headers. On Monday, October 29, 2012 at 10:48 AM, Fabien Potencier wrote: > I can see another issue. For some HTTP codes, you can/should/must add some headers or some content (like for the 406 status code). > Don't worry about the merge commit or the rebasing, I will take care of that myself when merging the PR. > > — > Reply to this email directly or view it on GitHub (symfony#5312 (comment)). > > >
2 parents 7322696 + 4e826c7 commit 27ee846

12 files changed

+439
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Exception;
13+
14+
/**
15+
* BadRequestHttpException.
16+
*
17+
* @author Ben Ramsey <ben@benramsey.com>
18+
*/
19+
class BadRequestHttpException extends HttpException
20+
{
21+
/**
22+
* Constructor.
23+
*
24+
* @param string $message The internal exception message
25+
* @param Exception $previous The previous exception
26+
* @param integer $code The internal exception code
27+
*/
28+
public function __construct($message = null, \Exception $previous = null, $code = 0)
29+
{
30+
parent::__construct(400, $message, $previous, array(), $code);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Exception;
13+
14+
/**
15+
* ConflictHttpException.
16+
*
17+
* @author Ben Ramsey <ben@benramsey.com>
18+
*/
19+
class ConflictHttpException extends HttpException
20+
{
21+
/**
22+
* Constructor.
23+
*
24+
* @param string $message The internal exception message
25+
* @param Exception $previous The previous exception
26+
* @param integer $code The internal exception code
27+
*/
28+
public function __construct($message = null, \Exception $previous = null, $code = 0)
29+
{
30+
parent::__construct(409, $message, $previous, array(), $code);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Exception;
13+
14+
/**
15+
* GoneHttpException.
16+
*
17+
* @author Ben Ramsey <ben@benramsey.com>
18+
*/
19+
class GoneHttpException extends HttpException
20+
{
21+
/**
22+
* Constructor.
23+
*
24+
* @param string $message The internal exception message
25+
* @param Exception $previous The previous exception
26+
* @param integer $code The internal exception code
27+
*/
28+
public function __construct($message = null, \Exception $previous = null, $code = 0)
29+
{
30+
parent::__construct(410, $message, $previous, array(), $code);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Exception;
13+
14+
/**
15+
* LengthRequiredHttpException.
16+
*
17+
* @author Ben Ramsey <ben@benramsey.com>
18+
*/
19+
class LengthRequiredHttpException extends HttpException
20+
{
21+
/**
22+
* Constructor.
23+
*
24+
* @param string $message The internal exception message
25+
* @param Exception $previous The previous exception
26+
* @param integer $code The internal exception code
27+
*/
28+
public function __construct($message = null, \Exception $previous = null, $code = 0)
29+
{
30+
parent::__construct(411, $message, $previous, array(), $code);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Exception;
13+
14+
/**
15+
* NotAcceptableHttpException.
16+
*
17+
* @author Ben Ramsey <ben@benramsey.com>
18+
*/
19+
class NotAcceptableHttpException extends HttpException
20+
{
21+
/**
22+
* Constructor.
23+
*
24+
* @param string $message The internal exception message
25+
* @param Exception $previous The previous exception
26+
* @param integer $code The internal exception code
27+
*/
28+
public function __construct($message = null, \Exception $previous = null, $code = 0)
29+
{
30+
parent::__construct(406, $message, $previous, array(), $code);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Exception;
13+
14+
/**
15+
* PreconditionFailedHttpException.
16+
*
17+
* @author Ben Ramsey <ben@benramsey.com>
18+
*/
19+
class PreconditionFailedHttpException extends HttpException
20+
{
21+
/**
22+
* Constructor.
23+
*
24+
* @param string $message The internal exception message
25+
* @param Exception $previous The previous exception
26+
* @param integer $code The internal exception code
27+
*/
28+
public function __construct($message = null, \Exception $previous = null, $code = 0)
29+
{
30+
parent::__construct(412, $message, $previous, array(), $code);
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Exception;
13+
14+
/**
15+
* PreconditionRequiredHttpException.
16+
*
17+
* @author Ben Ramsey <ben@benramsey.com>
18+
* @see http://tools.ietf.org/html/rfc6585
19+
*/
20+
class PreconditionRequiredHttpException extends HttpException
21+
{
22+
/**
23+
* Constructor.
24+
*
25+
* @param string $message The internal exception message
26+
* @param Exception $previous The previous exception
27+
* @param integer $code The internal exception code
28+
*/
29+
public function __construct($message = null, \Exception $previous = null, $code = 0)
30+
{
31+
parent::__construct(428, $message, $previous, array(), $code);
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Exception;
13+
14+
/**
15+
* ServiceUnavailableHttpException.
16+
*
17+
* @author Ben Ramsey <ben@benramsey.com>
18+
*/
19+
class ServiceUnavailableHttpException extends HttpException
20+
{
21+
/**
22+
* Constructor.
23+
*
24+
* @param int|string $retryAfter The number of seconds or HTTP-date after which the request may be retried
25+
* @param string $message The internal exception message
26+
* @param Exception $previous The previous exception
27+
* @param integer $code The internal exception code
28+
*/
29+
public function __construct($retryAfter = null, $message = null, \Exception $previous = null, $code = 0)
30+
{
31+
$headers = array();
32+
if ($retryAfter) {
33+
$headers = array('Retry-After' => $retryAfter);
34+
}
35+
36+
parent::__construct(503, $message, $previous, $headers, $code);
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Exception;
13+
14+
/**
15+
* TooManyRequestsHttpException.
16+
*
17+
* @author Ben Ramsey <ben@benramsey.com>
18+
* @see http://tools.ietf.org/html/rfc6585
19+
*/
20+
class TooManyRequestsHttpException extends HttpException
21+
{
22+
/**
23+
* Constructor.
24+
*
25+
* @param int|string $retryAfter The number of seconds or HTTP-date after which the request may be retried
26+
* @param string $message The internal exception message
27+
* @param Exception $previous The previous exception
28+
* @param integer $code The internal exception code
29+
*/
30+
public function __construct($retryAfter = null, $message = null, \Exception $previous = null, $code = 0)
31+
{
32+
$headers = array();
33+
if ($retryAfter) {
34+
$headers = array('Retry-After' => $retryAfter);
35+
}
36+
37+
parent::__construct(429, $message, $previous, $headers, $code);
38+
}
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Exception;
13+
14+
/**
15+
* UnauthorizedHttpException.
16+
*
17+
* @author Ben Ramsey <ben@benramsey.com>
18+
*/
19+
class UnauthorizedHttpException extends HttpException
20+
{
21+
/**
22+
* Constructor.
23+
*
24+
* @param string $challenge WWW-Authenticate challenge string
25+
* @param string $message The internal exception message
26+
* @param Exception $previous The previous exception
27+
* @param integer $code The internal exception code
28+
*/
29+
public function __construct($challenge, $message = null, \Exception $previous = null, $code = 0)
30+
{
31+
$headers = array('WWW-Authenticate' => $challenge);
32+
33+
parent::__construct(401, $message, $previous, $headers, $code);
34+
}
35+
}

0 commit comments

Comments
 (0)
0