Skip to content

Deprecate passing null as $message or $code to exceptions #40288

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class FileLoaderImportCircularReferenceException extends LoaderLoadException
{
public function __construct(array $resources, ?int $code = 0, \Throwable $previous = null)
{
if (null === $code) {
trigger_deprecation('symfony/config', '5.3', 'Passing null as $code to "%s()" is deprecated, pass 0 instead.', __METHOD__);

$code = 0;
}

$message = sprintf('Circular reference detected in "%s" ("%s" > "%s").', $this->varToString($resources[0]), implode('" > "', $resources), $resources[0]);

\Exception::__construct($message, $code, $previous);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class LoaderLoadException extends \Exception
*/
public function __construct(string $resource, string $sourceResource = null, ?int $code = 0, \Throwable $previous = null, string $type = null)
{
if (null === $code) {
trigger_deprecation('symfony/config', '5.3', 'Passing null as $code to "%s()" is deprecated, pass 0 instead.', __METHOD__);

$code = 0;
}

$message = '';
if ($previous) {
// Include the previous exception, to help the user see what might be the underlying cause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class AccessDeniedHttpException extends HttpException
*/
public function __construct(?string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

parent::__construct(403, $message, $previous, $headers, $code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class BadRequestHttpException extends HttpException
*/
public function __construct(?string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

parent::__construct(400, $message, $previous, $headers, $code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class ConflictHttpException extends HttpException
*/
public function __construct(?string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

parent::__construct(409, $message, $previous, $headers, $code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class GoneHttpException extends HttpException
*/
public function __construct(?string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

parent::__construct(410, $message, $previous, $headers, $code);
}
}
11 changes: 11 additions & 0 deletions src/Symfony/Component/HttpKernel/Exception/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ class HttpException extends \RuntimeException implements HttpExceptionInterface

public function __construct(int $statusCode, ?string $message = '', \Throwable $previous = null, array $headers = [], ?int $code = 0)
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}
if (null === $code) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $code to "%s()" is deprecated, pass 0 instead.', __METHOD__);

$code = 0;
}

$this->statusCode = $statusCode;
$this->headers = $headers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class LengthRequiredHttpException extends HttpException
*/
public function __construct(?string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

parent::__construct(411, $message, $previous, $headers, $code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ class MethodNotAllowedHttpException extends HttpException
*/
public function __construct(array $allow, ?string $message = '', \Throwable $previous = null, ?int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}
if (null === $code) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $code to "%s()" is deprecated, pass 0 instead.', __METHOD__);

$code = 0;
}

$headers['Allow'] = strtoupper(implode(', ', $allow));

parent::__construct(405, $message, $previous, $headers, $code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class NotAcceptableHttpException extends HttpException
*/
public function __construct(?string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

parent::__construct(406, $message, $previous, $headers, $code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class NotFoundHttpException extends HttpException
*/
public function __construct(?string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

parent::__construct(404, $message, $previous, $headers, $code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class PreconditionFailedHttpException extends HttpException
*/
public function __construct(?string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

parent::__construct(412, $message, $previous, $headers, $code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class PreconditionRequiredHttpException extends HttpException
*/
public function __construct(?string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

parent::__construct(428, $message, $previous, $headers, $code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ class ServiceUnavailableHttpException extends HttpException
*/
public function __construct($retryAfter = null, ?string $message = '', \Throwable $previous = null, ?int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}
if (null === $code) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $code to "%s()" is deprecated, pass 0 instead.', __METHOD__);

$code = 0;
}

if ($retryAfter) {
$headers['Retry-After'] = $retryAfter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ class TooManyRequestsHttpException extends HttpException
*/
public function __construct($retryAfter = null, ?string $message = '', \Throwable $previous = null, ?int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}
if (null === $code) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $code to "%s()" is deprecated, pass 0 instead.', __METHOD__);

$code = 0;
}

if ($retryAfter) {
$headers['Retry-After'] = $retryAfter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ class UnauthorizedHttpException extends HttpException
*/
public function __construct(string $challenge, ?string $message = '', \Throwable $previous = null, ?int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}
if (null === $code) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $code to "%s()" is deprecated, pass 0 instead.', __METHOD__);

$code = 0;
}

$headers['WWW-Authenticate'] = $challenge;

parent::__construct(401, $message, $previous, $headers, $code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class UnprocessableEntityHttpException extends HttpException
*/
public function __construct(?string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

parent::__construct(422, $message, $previous, $headers, $code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class UnsupportedMediaTypeHttpException extends HttpException
*/
public function __construct(?string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
{
if (null === $message) {
trigger_deprecation('symfony/http-kernel', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

parent::__construct(415, $message, $previous, $headers, $code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class HttpTransportException extends TransportException

public function __construct(?string $message, ResponseInterface $response, int $code = 0, \Throwable $previous = null)
{
if (null === $message) {
trigger_deprecation('symfony/mailer', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

parent::__construct($message, $code, $previous);

$this->response = $response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class MethodNotAllowedException extends \RuntimeException implements ExceptionIn
*/
public function __construct(array $allowedMethods, ?string $message = '', int $code = 0, \Throwable $previous = null)
{
if (null === $message) {
trigger_deprecation('symfony/routing', '5.3', 'Passing null as $message to "%s()" is deprecated, pass an empty string instead.', __METHOD__);

$message = '';
}

$this->allowedMethods = array_map('strtoupper', $allowedMethods);

parent::__construct($message, $code, $previous);
Expand Down