Skip to content

[HttpFoundation] Use Symfony exception for request unexpected values #51252

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 1 commit into from
Aug 4, 2023
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 @@ -14,6 +14,6 @@
/**
* Raised when a user sends a malformed request.
*/
class BadRequestException extends \UnexpectedValueException implements RequestExceptionInterface
class BadRequestException extends UnexpectedValueException implements RequestExceptionInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
*
* @author Magnus Nordlander <magnus@fervo.se>
*/
class ConflictingHeadersException extends \UnexpectedValueException implements RequestExceptionInterface
class ConflictingHeadersException extends UnexpectedValueException implements RequestExceptionInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class JsonException extends \UnexpectedValueException implements RequestExceptionInterface
final class JsonException extends UnexpectedValueException implements RequestExceptionInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
* Raised when a user has performed an operation that should be considered
* suspicious from a security perspective.
*/
class SuspiciousOperationException extends \UnexpectedValueException implements RequestExceptionInterface
class SuspiciousOperationException extends UnexpectedValueException implements RequestExceptionInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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\HttpFoundation\Exception;

class UnexpectedValueException extends \UnexpectedValueException
{
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpFoundation/InputBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpFoundation;

use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\Exception\UnexpectedValueException;

/**
* InputBag is a container for user input values such as $_GET, $_POST, $_REQUEST, and $_COOKIE.
Expand Down Expand Up @@ -87,7 +88,7 @@ public function getEnum(string $key, string $class, \BackedEnum $default = null)
{
try {
return parent::getEnum($key, $class, $default);
} catch (\UnexpectedValueException $e) {
} catch (UnexpectedValueException $e) {
throw new BadRequestException($e->getMessage(), $e->getCode(), $e);
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/Symfony/Component/HttpFoundation/ParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpFoundation;

use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\Exception\UnexpectedValueException;

/**
* ParameterBag is a container for key/value pairs.
Expand Down Expand Up @@ -140,7 +141,7 @@ public function getString(string $key, string $default = ''): string
{
$value = $this->get($key, $default);
if (!\is_scalar($value) && !$value instanceof \Stringable) {
throw new \UnexpectedValueException(sprintf('Parameter value "%s" cannot be converted to "string".', $key));
throw new UnexpectedValueException(sprintf('Parameter value "%s" cannot be converted to "string".', $key));
}

return (string) $value;
Expand Down Expand Up @@ -184,7 +185,7 @@ public function getEnum(string $key, string $class, \BackedEnum $default = null)
try {
return $class::from($value);
} catch (\ValueError|\TypeError $e) {
throw new \UnexpectedValueException(sprintf('Parameter "%s" cannot be converted to enum: %s.', $key, $e->getMessage()), $e->getCode(), $e);
throw new UnexpectedValueException(sprintf('Parameter "%s" cannot be converted to enum: %s.', $key, $e->getMessage()), $e->getCode(), $e);
}
}

Expand All @@ -211,7 +212,7 @@ public function filter(string $key, mixed $default = null, int $filter = \FILTER
}

if (\is_object($value) && !$value instanceof \Stringable) {
throw new \UnexpectedValueException(sprintf('Parameter value "%s" cannot be filtered.', $key));
throw new UnexpectedValueException(sprintf('Parameter value "%s" cannot be filtered.', $key));
}

if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
Expand All @@ -232,7 +233,7 @@ public function filter(string $key, mixed $default = null, int $filter = \FILTER
$method = ($method['object'] ?? null) === $this ? $method['function'] : 'filter';
$hint = 'filter' === $method ? 'pass' : 'use method "filter()" with';

trigger_deprecation('symfony/http-foundation', '6.3', 'Ignoring invalid values when using "%s::%s(\'%s\')" is deprecated and will throw an "%s" in 7.0; '.$hint.' flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.', $this::class, $method, $key, \UnexpectedValueException::class);
trigger_deprecation('symfony/http-foundation', '6.3', 'Ignoring invalid values when using "%s::%s(\'%s\')" is deprecated and will throw an "%s" in 7.0; '.$hint.' flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.', $this::class, $method, $key, UnexpectedValueException::class);

return false;
}
Expand Down
15 changes: 8 additions & 7 deletions src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\Exception\UnexpectedValueException;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Tests\Fixtures\FooEnum;

Expand Down Expand Up @@ -128,7 +129,7 @@ public function testGetAlphaExceptionWithArray()
{
$bag = new ParameterBag(['word' => ['foo_BAR_012']]);

$this->expectException(\UnexpectedValueException::class);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Parameter value "word" cannot be converted to "string".');

$bag->getAlpha('word');
Expand All @@ -149,7 +150,7 @@ public function testGetAlnumExceptionWithArray()
{
$bag = new ParameterBag(['word' => ['foo_BAR_012']]);

$this->expectException(\UnexpectedValueException::class);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Parameter value "word" cannot be converted to "string".');

$bag->getAlnum('word');
Expand All @@ -170,7 +171,7 @@ public function testGetDigitsExceptionWithArray()
{
$bag = new ParameterBag(['word' => ['foo_BAR_012']]);

$this->expectException(\UnexpectedValueException::class);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Parameter value "word" cannot be converted to "string".');

$bag->getDigits('word');
Expand Down Expand Up @@ -232,7 +233,7 @@ public function testGetStringExceptionWithArray()
{
$bag = new ParameterBag(['key' => ['abc']]);

$this->expectException(\UnexpectedValueException::class);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Parameter value "key" cannot be converted to "string".');

$bag->getString('key');
Expand All @@ -242,7 +243,7 @@ public function testGetStringExceptionWithObject()
{
$bag = new ParameterBag(['object' => $this]);

$this->expectException(\UnexpectedValueException::class);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Parameter value "object" cannot be converted to "string".');

$bag->getString('object');
Expand Down Expand Up @@ -359,7 +360,7 @@ public function testGetEnumThrowsExceptionWithNotBackingValue()
{
$bag = new ParameterBag(['invalid-value' => 2]);

$this->expectException(\UnexpectedValueException::class);
$this->expectException(UnexpectedValueException::class);
if (\PHP_VERSION_ID >= 80200) {
$this->expectExceptionMessage('Parameter "invalid-value" cannot be converted to enum: 2 is not a valid backing value for enum Symfony\Component\HttpFoundation\Tests\Fixtures\FooEnum.');
} else {
Expand All @@ -373,7 +374,7 @@ public function testGetEnumThrowsExceptionWithInvalidValueType()
{
$bag = new ParameterBag(['invalid-value' => ['foo']]);

$this->expectException(\UnexpectedValueException::class);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Parameter "invalid-value" cannot be converted to enum: Symfony\Component\HttpFoundation\Tests\Fixtures\FooEnum::from(): Argument #1 ($value) must be of type int, array given.');

$this->assertNull($bag->getEnum('invalid-value', FooEnum::class));
Expand Down